Promise

  • Represents an action that hasn't yet completed

  • solution to callback hell

var myPromise = new Promise(function(resolve, reject){
    var itemRequested = 10;

    if(itemRequested === 10 ){
        resolve("Happy to offer you 100 Rs.");
    }

    if(itemRequested === 'star'){
        reject("It's false hope");
    }
});

myPromise.then(function(res){
    console.log(res)
}, function(res){
    console.log(res)
})

Promise Chaining

catch

The catch callback is executed when the promise is rejected

Promise.all

Promise.all demo

If any promise is rejected the catch fires for the first rejection:

Promise.all demo

Promise.race

Triggers as soon as any promise is resolved

Ex - Native xhr request

fetch api return promise

Last updated

Was this helpful?