@js-util/promise-sleep

1.0.5 • Public • Published

Promise Sleep

Returns a promise, which resolve after the specified sleep itme (ms)

npm install

npm install --save @js-util/promise-sleep

Example usage

// Importing the module
const promiseSleep = require("@js-util/promise-sleep");

// Await sleep in ms
// 1000 ms -> 1 second
await promiseSleep(1000)

Its code

// Store a reference to the global setTimeout,
// in case it gets replaced (e.g. sinon.useFakeTimers())
const timeoutFunction = setTimeout;

/**
 * @param {Int} timeout in milliseconds to sleep by
 * @return a promise which resolves on timeout, returns no actual value
 */
function promiseSleep(timeout) {
    return new Promise((resolve) => {
        timeoutFunction(resolve, timeout);
    });
}

// Export the function
module.exports = promiseSleep;

Readme

Keywords

Package Sidebar

Install

npm i @js-util/promise-sleep

Weekly Downloads

4

Version

1.0.5

License

MIT

Unpacked Size

2.83 kB

Total Files

4

Last publish

Collaborators

  • picocreator