Programming Tutorial: Guide to Promises in TypeScript
Learning how promises work in TypeScript is critical if you want to work on Node.js, Angular 2, and many other functional programming systems. In this guide, I’m going to walk through exactly how we can work with promises in typescript. However, before we can talk about this I want to give more of an explanation on what promises are. Thankfully because of the way promises were created and how it’s named It’s pretty easy and straightforward to compare it to a real-world scenario and that is a promise in real life.
Real World Example
So imagine that I come to you and I say that I am going to mow your lawn. Now that is a promise that I’ve made a deal. And there are a couple things that could happen. When I say I’m going to mow your lawn there are three scenarios the first one is that I go outside and I start mowing that scenario 1 and in typescript, we would say that this is the pending status. This is kind of like what happens when we make an API request and it creates a promise or starts a promise and we’re waiting to see what is going to happen. Now taking the Mowing example further if I complete the Mowing process I’m going to come back and I’m going to say I’m done. And in that case, I was successful and I fulfilled my promise in typescript we would say that this is been resolved.
The keyword here is resolved. And that means that the promise proved to be true. Came back successful and everything went as planned. However what happens if I run away and I just start running down the street and I do not mow your lawn. That means that I did not complete the mowing process successfully. Now there is a special term for this and this is going to be called the reject status. And what this would be akin to is sending out an API request and having an error message come back either the server was down or any number of various errors. If you want to take this a step further in the analogy isn’t that always the case that maybe I lied about moving the lawn that also could be that I went outside to mow the lawn and there was no gas in the lawn mower or the lawn mower is broken or something like that. All kinds of different reasons why I may not be able to hold true to my promise just like there are all kinds of reasons why a program may not be able to fulfill the promise successfully. However, what I really want to kind of impart to you is the three stages of a promise. One is when it’s pending the next is when it’s successful which is what we’ll call resolved. And the last one is when it’s rejected.
As a side note if you’re keeping your compiler options in your Ts config file if you go back to some of your previous classes or your previous coursework such as the work that we did with classes or some of those things or even anyone that has that the let statement included that is going to now no longer work without put strict at the top so just as a side note if you go and try to rerun any of those programs and you get an error if you’re using the new compiler options you just have to put that at the top and you’ll be good to go.
Full written guide and source code: