Promise helps to run a function asynchronously and return either success or failure, when they are done processing. We can listen to success or failure state using then
callback.
There are 3 States in promise
-
Pending - Intial state which not fulfilled or rejected.
-
Fulfilled - async operation is completed successfully.
-
Rejected - async operation is failed.
Note: A promise can either fullfilled or rejected only once.
//Creating a promise object
var promise = new Promise(function(resolve, reject) {
//Making an ajax call
$.ajax({
url : 'https://api.github.com/users/gokulkrishh',
method: 'get'
})
.success(function (response) {
resolve(response); //Return response
})
.error(function (error) {
reject(error); //Return error
});
});
//Then callback to listen to resolve or rejected state
promise.then(function (response) {
console.log('Success -->', response);
},
//Reject callback
function (error) {
console.log('Error -->', error);
})
//To handle error
.catch(function (e) {
console.log(e);
});
To do multiple async calls you can use promise.all()
which takes array of promises. And once all of them are fullfilled, an array of values are returned in the same order as you called.
var promise = Promise.all([1, 2, 3, 4, 5]); //Passing an array of calls
promise.then(function (response) {
//All Response will be returned as an array in same order
response.forEach(function (value) {
console.log(value);
});
},
//Reject callback
function (error) {
console.log('Error -->', error);
})
//To handle error
.catch(function (e) {
console.log(e);
});