'use strict';
const race = (arr) => Promise.race(
arr.map((p, i) => p.then(v => [v, i]))
);
async function* promisGenerator(arrPromise) {
const promises = [...promisList];
while (promises.length) {
const [v, i] = await race(promises);
promises.splice(i, 1);
yield v;
}
}
const promisList = [
new Promise(resolve => setTimeout(resolve, 200, 15)),
new Promise(resolve => setTimeout(resolve, 600, 17)),
new Promise(resolve => setTimeout(resolve, 500, 42))
];
const start = Date.now();
(async () => {
const asyncGenerator = promisGenerator(promisList);
for await (let value of asyncGenerator) {
console.log(value, Date.now() - start);
}
})();
15 201
42 501
17 601
https://reactjs.org/tutorial/tutorial.html