Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

1.2 Using JavaScript Promises

In this lesson we will use promises to control the styling of a page when it loads. First we will use just one promise. After that, we will chain some promises together.

Code Snippet

Here is an example of creating a promise:

var myPromise = new Promise(function(resolve,reject){
    setTimeout(function() {
        resolve("Timeout elapsed");
    }, 10000);
});

We can then await the resolution of the promise with:

myPromise.then(function(result){
    console.log('Promise resolved: ' + result);
})
.catch(function(reason){
    console.log('Rejected because: ' + reason);
});

Related Links