async function delayFn(delay) {
await new Promise(function (resolve, reject) {
setTimeout(() => {
console.log(delay)
resolve()
}, delay)
})
}
delayFn(5000).then(() => {
delayFn(4000).then(() => {
delayFn(3000)
})
})
实现先过5秒执行打印5000,再过4秒执行打印4000,再过3秒执行打印3000
const myFun = async () => {
return new Promise(resolve => {
setTimeout(() => { // 用定时器模拟异步请求
const data = [
{ id: 1, name: 'xiaoming', age: 11 },
{ id: 2, name: 'xiaohong', age: 22 },
{ id: 3, name: 'xiaogang', age: 33 },
];
resolve(data);
}, 1000);
});
}
const myFun2 = async () => {
console.log(1);
res = await myFun();
console.log('res:', res);
}
myFun2();