/* * Retry the function fn up to max times until it successfully completes * without an error. Pause backoff * retry miliseconds between tries. */ const retry = (fn, {max = 5, backoff = 500} = {}) => Promise.resolve().then(() => { const rec = counter => Promise.try(() => fn(counter).catch(err => { if (counter <= max) { return Promise.delay(backoff * counter).then(() => rec(counter + 1)); } else { return Promise.reject(err); } })); // Init recursive function. return rec(1); })
tasks: { yield Promise.delay(9) t.equal(opts.val, demo.val, "task-a got initial `opts` object") order.push("a") }, yield Promise.delay(6) t.equal(opts.val, demo.val, "task-b got initial `opts` object") order.push("b") }, yield Promise.delay(3) t.equal(opts.val, demo.val, "task-c got initial `opts` object") order.push("c")
promise.delay(500).then(() => { return db.query( `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid();`
})); return metrics.report('escape', {freedman: 'valjean'}) .delay(5) .then(() => metrics.report('apprehended', {prisoner: 'valjean'})) .then(() => axios.create.restore());
tasks: { yield Promise.delay(2) t.equal(opts.val, int, "task-a got initial `opts` object") order.push("a") }, yield Promise.delay(1) t.equal(opts.val, int + 1, "task-b got mutated `opts` object") order.push("b") }, yield Promise.delay(0) t.equal(opts.val, int + 2, "task-c got mutated `opts` object") order.push("c")
waitForAliveInstances(count) { winston.debug('[Manager] waitForAliveInstances: count=%d', count); if (this._aliveInstances.length === count) { return Promise.resolve(); } else { return Promise .delay(2000) .then(() => this.waitForAliveInstances(count)); } }
// Add error with promise/delay api.use(function(err,req,res,next) { if (req.route === '/testErrorPromise') { let start = Date.now() Promise.delay(100).then((x) => { res.header('Content-Type','text/plain') res.send('This is a test error message: ' + req.testError1 + '/' + req.testError2) }) } else { next() } });
api.get('/download/test', function(req,res) { res.download('test/test.txt' + (req.query.test ? req.query.test : ''), err => { // Return a promise return Promise.delay(100).then((x) => { if (err) { // set custom error code and message on error res.error(501,'Custom File Error') } else { // else set custom response code res.status(201) } }) }) })
function pingRetry(options, retry, retryDelay) { if (!options || !options.hostname || !options.port || !retryDelay) { throw new Error('[pingRetry] should have hostname, port, retry and retryDelay'); } retry = retry || 0; winston.debug('[Pinger] pingRetry: hostname=%s / port=%d / retry=%d / retryDelay=%d', options.hostname, options.port, retry, retryDelay); return ping(options) .catch((err) => { if (retry > 0) { return Promise .delay(retryDelay) .then(() => pingRetry(options, retry - 1, retryDelay)); } throw err; }); }
.delay(10 * 1000) .then(() => { const instances = proxies.manager.instances;
api.get('/s3LinkError', async function(req,res) { stub.callsArgWith(2, 'getSignedUrl error', null) let url = await res.getLink('s3://my-test-bucket/test/test.txt', async (e) => { return await Promise.delay(100).then(() => {}) }) res.send(url) })
api.get('/s3LinkErrorCustom', async function(req,res) { stub.callsArgWith(2, 'getSignedUrl error', null) let url = await res.getLink('s3://my-test-bucket/test/test.txt', 60 ,async (e) => { return await Promise.delay(100).then(() => { res.error('Custom error') }) }) res.send(url) })
tasks: { yield Promise.delay(9) t.equal(opts.val, demo.val, "task-a got initial `opts` object") order.push("a") }, yield Promise.delay(6) t.equal(opts.val, demo.val, "task-b got initial `opts` object") order.push("b") }, yield Promise.delay(3) t.equal(opts.val, demo.val, "task-c got initial `opts` object") order.push("c")
tasks: { yield Promise.delay(2) t.equal(opts.val, int, "task-a got initial `opts` object") order.push("a") }, yield Promise.delay(1) t.equal(opts.val, int + 1, "task-b got mutated `opts` object") order.push("b") }, yield Promise.delay(0) t.equal(opts.val, int + 2, "task-c got mutated `opts` object") order.push("c")