1. Home
  2. Javascript
  3. Promise
#javascript#es6

The Promise object allows you to make an asynchronous call to another function. The 'resolve' function is called when the asynchronous call is successful otherwise it falls back to 'reject' function.

// Create promise
var promise = new Promise(function(resolve, reject) {
    if (...) {
        resolve("Promise resolved!");
    }
    else {
        reject(Error("Oh no!"));
    }
});

// Then you can use the promise
promise.then(
    function(result) {
        console.log(result); // "Promise resolved!"
    },
    function(err) {
        console.log(err); // Error: "Oh no!"
    }
);
copy
Full Javascript cheatsheet
Sitemap Git repository