It is not possible to explain all of the key concepts in a single article. So this will be a 4 part series that helps you lay the foundation of the major concepts in Promises.
What will be the output of the following code?
Will it throw an error, or will it execute?
It will execute and we will give the output- Printing: 1
As we are aware, TypeScript brings compile-time type checking to JS.
If we change the language to TypeScript and then try to compile the file, we will get the following error:
Up until this point, we have learned
Now we will look into some of the other methods in Promise API and when to use them.
Promise.all() method takes an iterable of promises as the input, and returns a single
This returned promise will only resolve when all of the input promises have resolved and its value will be an array of the results of the input promises.
If any of the input promises reject or throws an error, the returned promise will immediately…
In parts 1 and 2 of this series, we understood Why promises were introduced, What promises are, and how to create them. In this part, we will build on that by understanding how we can chain multiple promises together or simply Promise chaining.
This is an important concept to understand, one which is at the base of further asynchronous JS concepts like async/await.
Up until this point, we have only talked about a single asynchronous task being carried out. But what if, we have to do multiple asynchronous tasks in succession, each after the previous one finishes?
To understand that…
In part 1 of this series, we have understood why Promises were introduced. We know that the Promise API was introduced as a better paradigm to handle Asynchronous JS. Here we will understand What are promises and How to create promises with a simple example and we will, later on, look at a more practical example.
So what is a promise?
Imagine one morning at work, your colleague calls you up and asks you to do a favor. He needs you to go to the office parking lot and check whether there is an open spot. …