I need to send a request to multi-servers to see which server will response to my request and if any of them response, I will then further interact with this server. The simplest way is to send my request in sequence, something like this
async function probing(servers) {
for (const server of servers) {
const result = await fetch(server)
if (result.status == 200) {
return result
}
}
}
But I hope to speed up the probing process so I change my code to
async function probing(servers) {
results = await Promise.all(
servers.map(async server => {
return await fetch(server)
})
)
for (const result of results) {
if (result.status == 200) return result
}
}
But I still need to await all of promises to finish. What I really need is just if one of them has resolve I then return from my probing()
Is that possbile ?
---- update ----
Thank for the comments promise.any is the solution (and one-liner arrow function can be further simplified as following)
result = await Promise.any(
servers.map(server => fetch(server))
)
---- update 2 ----
I had thought Promise.any is the way to go and the end of the story. But unfortunately it is not! Promise.any is only available from Chrome 85+ & FF 79+, unlike Promise.all is available for any modern browser except for IE, check here https://v8.dev/features/promise-combinators
And my client needs me to support Chrome version from 2020, i.e. Chrome 80+, I tried to polyfill Promise.any with Babel but I failed.
We use babel6 and I failed to polyfill Promise.any with babel6. I tried to upgraded to babel7 (with npx babel-upgrade --write
and some twists) and now the bundled code using Promise.any() can't even work for chrome 88. I asked another question for that How do I polyfill Promise.any() using babel 7?
So now I just have to revert to Promise.all.
---- update 3 ----
I finally made polyfill Promise.any()
with Babel 7 work, the key is to using core-js@3
with correct babelrc setting (I am not sure I got them all correct), please refer to my question and answer there.