💾Promise.wait()

💾 replit:Promise.wait()

// ⭐ Promise.wait()
Promise.wait = function(seconds) {
    return new Promise((resolve, _) => {    // this Promise never rejects.
        // do not wait if `seconds` not positive.
        if (!(seconds > 0)) seconds = 0;
        // invoking resolve() with no arguments means 
        // that the Promise will always fulfill with value `undefined`.
        setTimeout(resolve, seconds * 1000);
    });
};

💈範例:

Promise
    .wait(2)                            // wait for 2 seconds
    .then(_ => console.log('hello'));

Last updated