const delay = (ms, val) => new Promise(res => setTimeout(res, ms, val));
async function* asyncGen() {
yield await delay(10, "alpha");
yield await delay(10, "beta");
yield await delay(10, "gamma");
}
async function run() {
const results = [];
const gen = asyncGen();
const [first, , third] = await Promise.all([
gen.next(),
gen.next(),
gen.next()
]);
results.push(first.value, third.value);
const p1 = Promise.resolve("x").then(v => v + "1");
const p2 = Promise.reject("err").catch(e => e + "2");
results.push(...(await Promise.all([p1, p2])));
console.log(results);
}
run();
Post #3233
2.43K
CHALLENGE
- ❤ 8
- 👍 3