promise,简单就就是回调的一种简化了回调地狱(如果多个调用是异步并且有结果依赖,那么就需要写成回调)。
async/await,需要成对使用,是对promise的更高级的抽象,
比如
runAsync1()
.then(function(data){console.log(data);return runAsync2();
})
.then(function(data){console.log(data);return runAsync3();
})
.then(function(data){console.log(data);
});
写成async/await,这样async函数返回里面只有await 修饰就会等待await修改的函数执行完成以后再执行下一句
async function fun1(){
var result1 = await runAsync1()
var result2 = await runAsync2(result1)
var result3 = await result2()
}fun1()//axios 返回的是promise对象
function runAsync1(){return axios({ method: 'get', url })
}//axios runAsync2 函数没有return,但是用了async和await修改就相当于放回了return promise
async function runAsync2() (ms) {console.log("112");await new Promise((resolve) => {setTimeout(resolve, ms);console.log("113");});console.log("115");};
//直接返回了promisefunction runAsync2() (ms) {console.log("112");var p = new Promise((resolve) => {setTimeout(resolve, ms);console.log("113");});console.log("115");return p;};}