在另一个async 函数调用 async 函数
async 函数返回的是一个promise对象,所有在另一个async函数调用 使用await, 等上一个async函数异步执行结束之后再执行后面代码
const sleep = (ms) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
})
}
const test1 = async () => {
for (let i = 0; i < 5; i++) {
console.log(i);
await sleep(1000);
}
return test1;
}
const test2 = async () => {
console.log(test2);
await test1().then(res => {
console.log(res)
});
console.log(test2_end)
}
test2();
console.log(page_end)
输出结果
// test2 // 0 // page_end // 1 // 2 // 3 // 4 // test1 // test2_end
async函数内部return语句返回的值,会成为then方法回调函数的参数, 如上test1函数返回 ‘test1’, 如没有return,then方法回调函数的参数为undefined
