作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在玩异步生成器,试图制作一个“ promise 排序”生成器,它接受一系列 promise ,并按照它们解决或拒绝的顺序一个接一个地产生 promise 。所以像:
async function* orderProms(prom_arr) {
// Make a copy so the splices don't mess it up.
const proms = [...prom_arr];
while (proms.length) {
// Tag each promise with it's index, so that we can remove it for the next loop.
const {prom, index} = await Promise.race(proms.map((prom, index) => prom.then(
() => ({prom, index}),
() => ({prom, index})
)));
proms.splice(index, 1);
yield prom;
}
}
const resAfter = (val, delay) => new Promise(res => setTimeout(() => res(val), delay));
const rejAfter = (val, delay) => new Promise((_, rej) => setTimeout(() => rej(val), delay));
const promises = [
resAfter("Third", 3000),
resAfter("First", 1000),
rejAfter("Second", 2000), // NOTE: this one rejects!
];
(async () => {
let ordered = orderProms(promises);
let done = false;
for (let next_promise = ordered.next(); !done; next_promise = ordered.next()) {
const next = await next_promise
.catch(err => ({done: false, value: `Caught error: ${err}`}));
done = next.done;
if (!done) console.log(next.value);
}
})()
yield prom
当
prom
被拒绝。
value
迭代器的结果。我不希望它被打开。这几乎就像被视为
yield await prom;
,但如您所见,没有
await
称呼。
async function* orderProms(prom_arr) {
// Make a copy so the splices don't mess it up.
const proms = [...prom_arr];
while (proms.length) {
// Tag each promise with it's index, so that we can remove it for the next loop.
const {prom, index} = await Promise.race(proms.map((prom, index) => prom.then(
() => ({prom, index}),
() => ({prom, index})
)));
proms.splice(index, 1);
yield prom;
}
}
const resAfter = (val, delay) => new Promise(res => setTimeout(() => res(val), delay));
const rejAfter = (val, delay) => new Promise((_, rej) => setTimeout(() => rej(val), delay));
const promises = [
resAfter("Third", 3000),
resAfter("First", 1000),
rejAfter("Second", 2000), // NOTE: this one rejects!
];
(async () => {
let ordered = orderProms(promises);
let done = false;
for (let next_promise = ordered.next(); !done; next_promise = ordered.next()) {
const next = await next_promise
.catch(err => ({done: false, value: `Caught error: ${err}`}));
done = next.done;
if (!done) console.log(next.value);
}
})()
最佳答案
It's almost like this is being treated as
yield await prom
. What is going on here?
how can I simply yield a rejected promise as-is from this generator.
try {
for await (const value of orderProms(promises)) {
console.log(value);
}
} catch(err) {
console.error('Caught error: ', err);
}
Promise.all
)orderProms
中或在向其中传递 promise 之前)并产生 promise 状态和值的元组for await (const value of orderProms(promises.map(prom =>
prom.catch(err => `Caught error: ${err}`)
))) {
console.log(value);
}
async
)生成器,您可以从中手动生成一个又一个 promise ,以便能够以您想要的方式使用它 关于javascript - 异步生成器 : Yielding a rejected promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62395474/
我是一名优秀的程序员,十分优秀!