回调地狱是指要执行多个异步操作时,为了保证按顺序向下执行,需要回调函数嵌套回调函数,
这样会导致代码难以理解 难以维护的情况
解决: Promise async/await 通过时间总线 通过发布/订阅模式 Generator
通过Promise的then方法解决回调地狱问题,代码会变成一行一行的,不会嵌套
eat().then((res) => {
console.log(res + '1')
return xizao()
}).then((res) => {
console.log(res + '1')
})
通过async,await函数将异步函数以同步的形式执行
async function run() {
let eat1 = await eat()
console.log(eat1 + '2')
let xizao1 = await xizao()
console.log(xizao1 + '2')
} run()
总结: