Use Template with Promise in TypeScript
May 12, 2016
In the post Asynchronous Programming with Async and Await, I compaired asyncchronous programming in C#, ES7(ES2016) and TypeScript. It surprises me that TypeScript has already implemented async
and await
and has a fully support. Here I’m going to talk a little bit more about how to use Template with Promise so as to support return type of await result.
For example, here’s last snippet:
1 | async function delay(ms: number) { |
How to support return value with defined type for the method deplay()
? Here’s the solution:
1 | function delay(ms: number) { |
Here we add the return type to Promise
, because Promise
use a template as the return type. When we call delay()
the return type is set to be number[]
.