Recently I learned about the Promise.allSettled()
method which generates a single promise from an array of promises similar to the Promise.all()
method, with the main difference that Promise.allSettled()
runs all the promises passed regardless if a promise is rejected.
I initially was using Promise.all()
for logic in a NodeJs Express application using TypeScript. However, after coming across an issue of breaking all promises once one of the promises was getting rejected, I decided to use Promise.allSettled()
as an alternative solution.
As a surprise, my VS Code wasn’t recognizing the allSettled()
method showing the following error:
Property 'allSettled' does not exist on type 'PromiseConstructor'
To solve the error “Property ‘allSettled’ does not exist on type ‘PromiseConstructor'”, add the library “es2020.promise” as part of the lib
key inside the tsconfig.json file.
{
"compilerOptions": {
"lib": [
"es2020.promise"
]
// ...
}
}
Note: You can look at the allSettled
type definition in the es2020.promise.d.ts file.
You could also use the es2020 library instead of specifying the es2020.promise library in your tsconfig.json file.
{
"compilerOptions": {
"lib": [
"es2020"
]
// ...
}
}
Finally, in case you use VS Code as your IDE of choice and TypeScript still doesn’t recognize the Promise.allSettled()
method, close and reopen VS Code. After that, you will no longer see the error as well as having the allSettled()
option via IntelliSense once you type Promise.