Skip to content
Related Articles

Related Articles

How to wrap setTimeout() method in a promise ?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 22 Nov, 2019

To wrap setTimeout in a promise returned by a future. We can wrap setTimeout in a promise by using the then() method to return a Promise. The then() method takes upto two arguments that are callback functions for the success and failure conditions of the Promise. This function returns a promise. There can be two different values if the function called onFulfilled that’s mean promise is fulfilled. If the function onRejected called that means promise is rejected.


Promise.then(onFulfilled, onRejected)

Below example will illustrate the approach:


<!DOCTYPE html>
        How to wrap setTimeout in a promise?
<body style="text-align:center;">
    <h1 style="color:green;">
        How to wrap setTimeout in a promise?
    <button onclick="change()">
        Click to print
    function change() {
        return new Promise(function(resolve, reject) {
            // Setting 2000 ms time
            setTimeout(resolve, 2000);
        }).then(function() {
            console.log("Wrapped setTimeout after 2000ms");


  • Before Clicking the button:
  • After clicking the button:

Reference:Promise.prototype.then() Method

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!