From callback hell to promises
Promise in javascript are objects who are used to treat asynchronous code. A promise represent a value who can be available now, in the future or never.
How works a promise
A promise can have different states:
Pending
The pending states means that the Promise simply is waiting for a certain event.
Resolved or not
After that a certain event happened the Promise can now be either resolved or reject depending on the result of our callback function inside our Promise.
Consume the promise
From now our Promise should return a result whether it is positive or negative we have a data in result, so we can access this result outside of our Promise with some new keywords that we will see.
Promise in demo
Let's see with examples how you can create Promise.
Create your promise
const getId = new Promise( (resolve, reject) => {
setTimeout( () => {
resolve( [523, 883, 432, 974] );
}, 1500 );
} );
- To create
Promisewe can simply create an object by using the classPromise, remember that aPromiseis simply an object with methods and properties. - Immediately after created your new object you have to pass a callback function with two arguments which will the result of your
Promiseremember yourPromisecan be either resolved or reject the result will be in this parameters. - From now the structure of your
Promiseis created you can now add some asynchronous code inside yourPromise, for example thesetTimeoutfunction. - To say that a
Promiseis successful or not we use our parameters which are by the way functions, the first one must be called when yourPromiseis successful, the second when she's not successful. In our example when we're gonna called ourgetId Promisewe know that in every case thesetTimeoutfunction will be executed so thePromisewill be successful. - In our example our code is really simple but in most cases
Promiseare used to received data from server or AJAX requests etc.. So therejectparameter can be used to check if any errors happened during the process.
Retrieve the result of your promise
const getId = new Promise( (resolve, reject) => {
setTimeout( () => {
resolve( [523, 883, 432, 974] );
}, 1500 );
} );
getId
.then( id => {
console.log( id ); // [523, 883, 432, 974]
} )
.catch( error => {
console.log(error);
} );
- In function of the result of your
Promiseto retrieve the result of this one you have to use thethenorcatchfunction, The first one will be used if the result is resolved and the second if it's not. - Once again in each case you have to use callback function with argument that will be the result value of your
Promise, so ourthenmethod will return our array which is inside of ourresolvefunction. - We also use the
catchmethod in case of ourPromisereturn arejectresult but in this case this function will never be called because we have aresolvefunction inside ourPromisebut just to show that you can write your code like this and it will be correct. - As you can see our code is more clear and organize it's more simple to understand, of curse you can add as many as you want
Promiseto do some asynchronous code.
Summary
- A
Promiseis an object who keep track about a certain event who has already happen or not. - The
Promisedetermine what happens when a certain event happened. - A
Promiseimplement the concept of a future value that we're expecting.
Current errors
This is a list of the main errors that you can meet:
Note: I'm not a wizard there is maybe some issue that you notice above so fell free to open an issue in the github repo if you find a new error not mentioned above.