await 可以同步执行,但是有时候为了提升性能,反而需要并发多个await同步执行,此时需要用到promise.all
DEMO如下
async function doit() { var list = []; list.push(sayHello('a1')) list.push(sayHello('a2')); var result = await Promise.all(list); console.log(result); console.log('over') } async function sayHello(name) { await new Promise(function (resolve) { setTimeout(function () { console.log(name + new Date()); resolve(name); }, 1000) }) } doit();
来源:51CTO
作者:isaisai
链接:https://blog.csdn.net/ISaiSai/article/details/101694918