A promise is an instance (object) of the Promise class (constructor). To create a promise, we use new Promise(_executor_) syntax and provide an executor function as an argument. This executor function provides a means to control the behavior of our promise resolution or rejection.

In TypeScript, we can provide the data type of the value returned when promise fulfills. Since the error returned by the promise can take any shape, the default data type of value returned when the promise is rejected is set to any by the TypeScript.

To annotate the resolution value type of the promise, we use a generic type declaration. Basically, you promise a type with Promise constructor in the form of new Promise<Type>() which indicates the resolved value type of the promise. But you can also use let p: Promise<Type> = new Promise() syntax to achieve the same.

💡 We have discussed generics classes in detail in the Generics lesson.

Image for post

(promise.ts)

In the above example, findEven is a promise which was created using the Promise constructor that resolves after 1 second. The resolved data type of this promise is number, hence the TypeScript compiler won’t allow you to call resolve function with a value other than a value of type number number.

The default type of the promise’s rejection value is any, hence calling reject function with any value is legal. This is the default behavior of TypeScript, and you can find the discussion thread here if you have your own opinions.

Since we have provided the number as the data type of successful promise resolution, the TypeScript compiler will provide the number type to the argument of value argument of the then callback method.

The callback provided in the then method is executed when the promise is resolved and the callback provided in catch method is executed when it rejects or some error while resolving the promise. The finally method registers a callback that executes when promise either resolves or rejects.

If the TypeScript compiler complains about the finally method, that means your TypeScript compiler doesn’t import type definitions for the finally method. This method was introduced in ES2016, hence it’s quite new. Other features of the Promise API used in this lesson are pretty new, hence make sure your tsconfig.json file has all the new libraries loaded.

Image for post

(tsconfig.json)

In my tsconfig.json, I have loaded the ES2020 standard library. This provides support for all the JavaScript feature up until ES2020. If you want to know more about the tsconfig.json file or standard libraries, please read the Compilation lesson (coming soon).

Promise Chaining

The thencatch and finally methods return a promise implicitly. Any value returned by these callback functions is wrapped with a promise and returned, including undefined. This implicit promise is resolved by default unless you are deliberately returning a new promise from these methods that could fail.

Hence you can append thencatch or finally methods to any of the previous thencatch or finally method. If an implicit promise is returned by one of these methods, then the resolved value type of this implicit promise is the type of the returned value. Let’s see a quick example.

Image for post

(promise-chaining.ts)

We have modified the previous example and added another then method to the first then method. Since the first then method returns a value of type string, the implicit promise returned by this method will be resolved with a value of the type string. Hence, the second then method will receive value argument of type string as you can see from the results.

#typescript #javascript #nodejs #programming #deno

TypeScript — Promises and Async/Await
1.60 GEEK