【发布时间】:2020-11-05 19:12:51
【问题描述】:
我有一个独特的情况,两个 Promise 在 Promise.all 中一起运行。 但其中一个承诺需要很长时间,因此我没有得到任何结果。 除了一个,其他承诺正在得到解决。 我想用错误消息拒绝需要很长时间(例如:如果超过 60 秒)的承诺,以便我可以从 Promise.all 获得响应。
例如::
const [client1Prices, client2Prices] = await Promise.all([
this.client1.getSimulationPrices({
hourPay: journey.hourPay,
jobType: journey.jobType,
salary: journey.salary,
weeklyHours: journey.weeklyHours,
}),
this.client2.getSimulationPrices({ // takes more than 60 sec and i want to reject this promise
hourPay: journey.hourPay,
jobType: journey.jobType,
salary: journey.salary,
weeklyHours: journey.weeklyHours,
})
]);
this.client2.getSimulationPrices 需要花费大量时间来解决,因此 Promise.all 没有给我任何结果。我想在 60 秒内拒绝这个,这样我就可以得到 Promise.all 的响应。
请建议如何处理这种情况?
【问题讨论】:
-
如果第二个 promise 被拒绝,您是否期望能够访问第一个 promise 的结果?
-
是的,我期待第一个承诺的结果和第二个承诺的一些错误消息。
-
与
Promise.all()如果一个人拒绝,他们都会拒绝。 (本质上) -
这就是为什么我想用某种形式的错误消息来处理需要很长时间的承诺,如果它需要更多时间,以便我可以继续做进一步的任务。
-
我的方法失败了。什么是最好的解决方案?
标签: javascript node.js promise promise.all