await async 中如何做到并行处理?
例如:
function getName(name){
return Promise(function(){
resolve(name + '123');
});
}
(async ()=>{
var res = [];
for(const name of names){
res[name] = await getName(name);
}
})();
3 回复
可以await一个promise.all
https://github.com/magicdawn/promise.map
pmap(array, async item => {
// ...
}, concurrency)
function getName(name) { return new Promise(function(resolve, reject) { resolve(name + ‘123’); }); }
// jshint ignore: start (async function(names) { let res = await Promise.all(names.map(item => getName(item))); console.log(res); })([‘a’, ‘b’, ‘c’]); // jshint ignore: end