A promise based trier
Do one thing and do it well many times.
npm install trier-promise
var tp = require('trier-promise');
tp({
action: someFunctionThatReturnsAPromise,
timeout: 10000,
interval: 1000
})
.then(function(value) {
// the value from the first success
})
.catch(function(error) {
// error.message === 'TIMEOUT'
});
If the asynchronous function takes a while, you may not want overlapping calls.
tp({
action: aFunctionWithStdAsyncSignature,
timeout: 10000,
sleep: 1000,
async: true // note: this is needed so that tp knows to call with callback and wait
})
.then(function(value) {
// the value from the first success
})
.catch(function(error) {
// error.message === 'TIMEOUT'
});
tp({
action: aFunctionThatReturnsImmediately,
limit: 10,
sleep: 1000
})
.then(function(value) {
// the value from the first success
})
.catch(function(error) {
// error.message === 'LIMIT'
});
tp({
action: someFunctionThatReturnsAPromise,
until: function() { return Math.random() < 0.1 },
sleep: 1000
})
.then(function(value) {
// the value from the first success
})
.catch(function(error) {
// error.message === 'UNTIL'
});
Sometimes you want to start polling rapidly but slow down for subsequent calls.
So instead of using a Number value as the interval (or sleep), specify a function that returns a Number.
var interval = 10;
tp({
action: aFunctionWithStdAsyncSignature,
timeout: 600000, // 10 minutes
interval: function() { return interval *= 2; },
async: true // note: this is needed so that tp knows to call with callback and wait
})
.then(function(value) {
// the value from the first success
})
.catch(function(error) {
// error.message === 'TIMEOUT'
});