Categories
TypeScript Answers

How to use Promise.all() with TypeScript?

Spread the love

Sometimes, we want to use Promise.all() with TypeScript.

In this article, we’ll look at how to use Promise.all() with TypeScript.

How to use Promise.all() with TypeScript?

To use Promise.all() with TypeScript, we can use it as we do with JavaScript.

For instance, we write

const [foo, bar] = await Promise.all([fooPromise, barPromise]);

in an async function.

We call Promise.all to return a promise with an array of the resolved values from the array of promises we passed into Promise.all.

The TypeScript compiler will infer the type automatically and make sure they’re consistent between both objects so we don’t have to specify them ourselves.

Conclusion

To use Promise.all() with TypeScript, we can use it as we do with JavaScript.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *