使用Promise 实现每隔1s输出1,2,3……
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| var a = 0; function timeCout(){ return new Promise((resolve)=>{ a++; console.log(a); setTimeout(()=>{ resolve(); },1000); }) }
async function main(){ await timeCout(); main(); }
main();
|
1 2 3 4 5 6 7 8
| const arr = [1, 2, 3] arr.reduce((p, x) => { return p.then(() => { return new Promise(r => { setTimeout(() => r(console.log(x)), 1000) }) }) }, Promise.resolve())
|