promise-timers v1.0.1
DEPRECATED (This project has been moved to https://github.com/AlexanderElias/promise-tool)
Promise Timers
A promised library of timers for Node.js and the browser. If you know the WindowTimers and promises you know how to use PromiseTimers.
Install
npm install promise-timers
API
PromiseTimers.setTimeout-delayThe number of milliseconds to wait before calling resolve. -...argsOptional arguments to pass when the resolve is executed.PromiseTimers.setInterval-delayThe number of milliseconds to wait before calling resolve. -methodA function that repeats on each interval. This function will fire upon each interval unless one of the following returns are implemented. - Return Value Actions -resultAny valid JavaScript error type. Will fire the reject and pass the error. -resultA boolean that calls resolve if true or reject if false. -resultAny thing returned besidesnull,undefined,false, and a validErrortype will resolve with that return value as the first argument. -result<Null, Undefined> Both are ignored and will not trigger the resolve or reject. -...argsOptional arguments to pass when the resolve is executed.PromiseTimers.setImmediate-...argsOptional arguments to pass when the resolve is executed.PromiseTimers.clearTimeout-timeoutA Timeout object as returned by setInterval().PromiseTimers.clearInterval-intervalA Interval object as returned by setInterval().PromiseTimers.clearImmediate-immediateAn Immediate object as returned by setImmediate().
Example
const PromiseTimers = require('promise-timers');
const delay = 500;
PromiseTimers.setTimeout(delay).then(function (args) {
// this refers to timeout
console.log(args);
console.log('timeout done');
});
var i = 0;
function method () {
// this refers to interval
if (i > 5) {
return true;
} else {
console.log(i);
i++;
}
};
PromiseTimers.setInterval(delay, method).then(function (args) {
// this refers to interval
console.log(args);
console.log('interval done');
});
PromiseTimers.setImmediate().then(function (args) {
// this refers to immediate
console.log(args);
console.log('immediate done');
});