Promise in Java script

Promising asynchronous functions in JavaScript

Thursday, September 13, 2018

What is a promise?

A promise is a JavaScript object that can be used to track the execution of an asynchronous function. An example of a asynchronous function is a http-request.

Imagine, you want to get certain data from an API and use this data in a web application. In your code you connect to the API with a http-request. A http-request takes some time to resolve. But JavaScript doesn’t wait. The application continues to run. If you are not careful this can give errors down the line.

This is where promises come is. A promise can be used to execute code after an asynchronous function has run.

How to make a promise?

let promiseObject = new Promise((resolve , reject) =>{
   //do something async
   resolve(value);
});

Here you can see an example of the constructor of a promise. The argument for this constructor is a function. You can write your asynchronous here. This function has 2 arguments “resolve” and “reject”. With these arguments you can, as the names say, resolve and reject the promise.

When the function in the promise a success, you can call resolve()with as arguments a return value. If you call resolve(value), you say that the promise has been resolved and finished.

If there is a error in your function, than you can call reject(). You can also call the reject() function with 1 argument. You can give this argument the reason why the application failed. An example is an error message.

How to use a promise?

So you have a promise object, what now? A promise object has 2 functions that are interesting to use:

.then(onResovle, [onReject]) , .catch(onReject)

And this is how you can use them

promiseObject.then((value) =>{}).catch((error)=>{});
.then(onResovle, [onReject])

This has, as you can see, 2 arguments. The first argument is a function that is executed if the promise is successfully resolved. The function has as an argument the value that you paste in the resolve() function of the promise.

The second argument of the .then() function is also a function. This function is executed when the promise is rejected or if an exception is thrown when the promise is executed. This is optional argument and can be replaced by the .catch() function (see later). The function, that is used as argument, has 1 argument. This argument is the reason why the promise has been rejected (an error message for example). 

.catch(onReject) is a alternative for the optional second argument of .then(onResovle, [onReject]).It takes 1 function as argument. This function is only executed when the promise is rejected or if an exception is thrown in the promise. The function has only 1 argument. This argument is the reason that the promise has been rejected.

An example:

let object = new Promise((resolve , reject) =>{
	setTimeout(()=>{
  	resolve("hello world")
	}, 2000)
});
 
object
.then((value) =>{console.log(value)})
.catch((error)=>{console.log("a unexpected error")})

More information

There are just the basics of a promise. There are more things you can do with promises. If you want to know more, you can visit this website : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global...

Source:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global...

 

Laura