2.5.1 • Published 6 years ago

promise-to-retry v2.5.1

Weekly downloads
-
License
BSD-3-Clause
Repository
-
Last release
6 years ago

PromiseToRetry Codeship Status for firmfirm/promise-to-retry

Utility functions that helps dealing with Javascript Promises.

  • ensure(fn => Promise, cb) is a helper function to retry some action returning Promise until it succeeds.
  • setTimeoutToPromise(fn => Promise, wait) waits for specified amount of time before invoking your function. Resolves with promise resulting from provided fn.
  • new Queue().queue(fn => Promise) invokes functions one by one (waits for firstly enqueued functions to resolve their results)

Usage:

bower install --save promise-to-retry

<link rel="import" href="bower_components/promise-to-retry/promise-to-retry.html">
PromiseToRetry.ensure(function() {
  // return your promise that might fail,
  // etc. instead of this it could do Promise.reject
  return Promise.resolve('ensured');
})
.then(function(result) {
  console.log(result); // 'ensured'
})
.catch(function(error) {
  // this can never happen
});

PromiseToRetry.setTimeoutToPromise(function() {
  // this is delayed by 1000ms
  return Promise.resolve('delayed');
}, 1000)
.then(function(result)) {
  console.log(result); // 'delayed'
}
.catch(function(error) {
  // this can still happen
});

var queue = new PromiseToRetry.Queue();
var first = queue.queue(() => new Promise(...));
queue.queue(() => {
  // this will not get called until first promise resolves
  return Promise.resolve('second');
});