Purpose drives leaders

Technological progress is taking place so quickly that none of us is able to maintain a constant overview. In a world that is characterised by innovation, disruptive brands that grow massively almost…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




A Quick Look on JavaScript Promise API

We know synchronous execution is kind of line by line execution. Here only execute next line of statement after the successful completion of the previous line.

But in asynchronous programs, the asynchronous statement will be schedule and start the next line of statement without wait to get complete previous execution’s completion.

And so asynchronous tasks are little bit difficult to handle when compared to synchronous tasks. It will be good if we can handle asynchronous tasks and collect its success and error responses just like JavaScript error handling method try and catch. Don’t worry, yes, we can do that with Promise.

Promise is using for handle asynchronous tasks. In promise, you will have a hook on both resolve and reject methods and then you can collect success and error responses while invoking the function.

In this article, I’m going to discuss about JavaScript Promise API.

A new promise can be created with new keyword on Promise object. We can pass a executor function with two arguments, resolve and reject. The resolve will invoke once the executor function completes execution successfully. And the execution will ignore and invoke the reject function If thrown any error in execution function.

We can handle three states of a asynchronous execution using Promise, those are

A corresponding handler then will invoke when a pending Promise fulfilled. And a corresponding handler catch will invoke when the pending Promise rejected. We can chain then and catch handler functions to the Promise while invoking, like below.

Add a comment

Related posts:

Time to let go

Tears wanting to come down the baby face, reminding myself I’m not a baby anymore. For a moment I thought you were coming to ask me about school, but instead you ask me about boxing goods so you can…

The Streamlining Energy Efficiency for Schools Act

The Streamlining Energy Efficiency for Schools Act, a Medium series by RABIOU YARI