异步函数 async await
function a(n=0) { return new Promise((resolve, reject) => { setTimeout(() => { let res = 'a'; if(n!=0){ res = 'a'+n; } resolve(res); }, 1000); }) } function b(){ return new Promise((resolve,reject)=>{ setTimeout(() => { resolve('b'); }, 2000); }) } function c() { return new Promise((resolve, reject) => { setTimeout(() => { resolve('c'); }, 3000); }) } //单个异步 async function sign(){ let _a = await a(); console.log(_a); } //多个异步 async function all(){ let _a = a(); let _b = b(); let _c = c(); let _all = await Promise.all([_a,_b,_c]); console.log(_all); } //遍历多个异步 async function arr_each(){ let arr = [1,2,3]; let res = []; for (let i of arr) { res.push(await a(i)); } console.log('遍历多个异步 结束',res); } arr_each();