Coding / Programming Videos

Post your favorite coding videos and share them with others!

Programming Tutorial: Guide to Promises in TypeScript

Download Video link >

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.

So those are the three things and I’m going to keep this comment here in the source code just say have access to it. Can that analogy in your mind because when I was learning about promises I heard kind of a similar analogy and that was one of the things that really made it clear to me. Now in order to use promises in the version of typescript, we’re using we need to put something at the top of the file and actually at the very top of the file. Just so it’s clear that’s being used. And this is going to be called use strict. Now what this means is that we’re going to be using the strict definition or the strict process in Javascript if you have any experience with javascript and you probably have heard about the strict process or the strict syntax. That’s what we’re doing here. It doesn’t really change how we’re going to be building the program. It’s simply something that is required for the typescript compiler for this ES6 version.

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:

TypeScript Tutorial: Practical Way to Work with Promises

Follow me:





View Comments source >

Leave a Reply

Please Login to comment
3 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
0 Comment authors
Recent comment authors
newest oldest most voted
Notify of

… [Trackback]

[…] Find More on|Find More|Read More Informations here|Here you can find 69012 additional Informations|Infos on that Topic: […]


… [Trackback]

[…] Info to that Topic: […]


… [Trackback]

[…] There you can find 48216 additional Information to that Topic: […]

Translate »