Reputation: 89993
Typescript 4.2.2
Given:
const firstPromises: Promise<First>[] = ...;
const secondPromises: Promise<Second>[] = ...;
return Promise.all([firstPromises, secondPromises]).
then([first, second] => {
// do something
}
I expect first
and second
to be of type First[]
and Second[]
respectively. Instead, the output types remain as Promise<First>[]
and Promise<Second>[]
. Any ideas?
Upvotes: 1
Views: 323
Reputation: 186994
You can't pass a nested array of promises to Promise.all
. It doesn't know how to handle that, so it just passes it right through to then
because it doesn't see any promises. It jsut sees an array, which is doesn't look inside of.
But you can just call Promise.all
on each sub array of promises to package that up into a single promise. Then pass each of those to Promise.all
to create a promise for both sets.
const getBool = async () => true
const getString = async () => "foo"
function foo() {
const firstPromises: Promise<boolean>[] = [getBool(), getBool()];
const secondPromises: Promise<string>[] = [getString(), getString()];
return Promise.all([
Promise.all(firstPromises),
Promise.all(secondPromises)
]).then(([first, second]) => {
// first: boolean[]
// second: string[]
})
}
Upvotes: 1