使用async & await & Promise 实现队列任务

let tmp = [];
(
async () => {
for (let i = 1; i < 11; i++) {
await new Promise((resolve, reject) => {
setTimeout(() => {
tmp.push(i);
console.log('第' + i + '条记录');
if (i === 10) {
console.log('tmp', tmp)
} else {
resolve()
}
}, Math.ceil(Math.random()*100));
})
}
}
)()

你可能感兴趣的:(使用async & await & Promise 实现队列任务)