admin管理员组文章数量:1303068
Promise.race(promise1, promise2, ...)
returns a promise with the resolve/reject result for the "fastest" promise from the list.
What happens to the other promises, i.e. those that "lose" the race?
Testing with Node.js seems to indicate that they continue running.
This seems consistent with the fact that there is no way to "kill" a promise that I know of.
Is this correct ?
Promise.race(promise1, promise2, ...)
returns a promise with the resolve/reject result for the "fastest" promise from the list.
What happens to the other promises, i.e. those that "lose" the race?
Testing with Node.js seems to indicate that they continue running.
This seems consistent with the fact that there is no way to "kill" a promise that I know of.
Is this correct ?
Share Improve this question edited Sep 26, 2024 at 14:59 Dan Dascalescu 152k64 gold badges333 silver badges420 bronze badges asked May 19, 2021 at 13:26 chetzacoaltchetzacoalt 1571 silver badge10 bronze badges 3- 3 Don't know of too many races where the runners stop when someone wins. Just sayin'. – Jared Farrish Commented May 19, 2021 at 13:29
- 3 Correct. Because promises don't "run". They are just markers for some async operation. The only thing you can get from a promise is the end result of the operation - you cannot affect the operation. – VLAZ Commented May 19, 2021 at 13:29
- 1 Does this answer your question? Is it a documented behavior that Promise.all and Promise.race effectively make all promises "handled"? – Manuel Spigolon Commented May 20, 2021 at 8:25
2 Answers
Reset to default 10All promises in a race
will continue running even after the first one crosses the finish line -
const sleep = ms =>
new Promise(r => setTimeout(r, ms))
async function runner (name) {
const start = Date.now()
console.log(`${name} starts the race`)
await sleep(Math.random() * 5000)
console.log(`${name} finishes the race`)
return { name, delta: Date.now() - start }
}
const runners =
[ runner("Alice"), runner("Bob"), runner("Claire") ]
Promise.race(runners)
.then(({ name }) => console.log(`!!!${name} wins the race!!!`))
.catch(console.error)
Promise.all(runners)
.then(JSON.stringify)
.then(console.log, console.error)
Alice starts the race
Bob starts the race
Claire starts the race
Claire finishes the race
!!!Claire wins the race!!!
Alice finishes the race
Bob finishes the race
[
{"name":"Alice","delta":2158},
{"name":"Bob","delta":4156},
{"name":"Claire","delta":1255}
]
The race metaphor must not be taken too literally, but you can think of it as follows:
- The runner is an asynchronous process (like fetching a document from a web server or database, awaiting user input, or waiting for time to pass).
- The promise is a time-taker that reports when the runner crosses the finishing line (when the document or user input bees available or the time has passed).
- The race winner is determined when the first time-taker reports the finishing result (aka winning time).
Even when a race has been won, the other time-takers involved (other than the one reporting the winning time) must continue watching their runners1, because they may be taking part in another race that they might still win. See the following example.
var over;
function runner(ms) {
return new Promise(function timetaker(resolve, reject) {
setTimeout(function() {
if (!over) resolve(`Finished after ${ms} ms`);
}, ms);
});
}
function endofrace(result) {
console.log(result);
//over = true;
}
var veryslow = runner(2000);
var slow = runner(1000);
var fast = runner(500);
Promise.race([slow, fast]).then(endofrace);
Promise.race([slow, veryslow]).then(endofrace);
If you want to avoid that behavior ("disqualify losers from other races"), you must program "race-is-over" awareness into the promise, like the over
variable in the example. If you bring in the mented-out line, the 1000 ms racer will no longer be able to win the second race.
(More precisely, if over = true
the runner setTimeout
will still finish, but the time-taker will not report any more. Race-is-over awareness is with the time-taker, not the runner. But in the case of a web server request, race-is-over awareness could instead be with the runner so that the request is aborted when the race is over.)
1 Another weakness of the metaphor: It is wrong to assume that the time-takers (promises) consume any resources while watching the runners.
本文标签: javascriptWith Promiserace()what happens to losing promisesStack Overflow
版权声明:本文标题:javascript - With Promise.race(), what happens to losing promises? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741726175a2394629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论