promise-wait-until

Mini util function, who wait until condition function return 'true' and resolve promise.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
promise-wait-until
201.0.05 years ago5 years agoMinified + gzip package size for promise-wait-until in KB

Readme

Build Status() Coverage Status() node() MIT License()
NPM
Sponsor
import waitUntil from 'promise-wait-until';

async function fn(id){
  return await waitUntil(async () => {
    const result = await db.findOne({ _id: id });

    if (!result) {
      return result;
    }
  },
  1000,
  {
    message: 'Document with this id wasn\'t created, sorry =(',
    time: 60 * 60 * 1000,
  });
}

Every 1000ms invoke checker function (first check immidiantly after run waitUntil), wait for end of promise (if he return promise). If checker return value, who can convert to true (as ==), end waitUntil and return value in promise, otherwise continue to check every 1000 ms.

If checker function throw exception or reject promise, promise from waitUntil will be rejected.

If in third param sended number of ms for timeout (or object with message and time), if checker function don't return 'true' value before timeout is end, reject promise with error message from object or 'Time is up'.