setTimeout
as a native Promise
.
Install using npm:
$ npm install timeout-as-promise
const delay = require('timeout-as-promise');
delay().then(function() {
console.log('nextTick!');
});
delay(5000).then(function() {
console.log('5 seconds have passed!');
});
delay(5000, 'Jim').then(function(value) {
console.log('my name is ' + value);
});
Promise.resolve(42)
.then(function(result) {
return delay(150, result);
})
.then(function(result) {
console.log('result is ' + result); //=> 42
});
const delay = require('timeout-as-promise');
async function stop() {
console.log('starting up');
await delay(2000);
console.log('2 seconds have passed');
}
stop();
MIT