All files / utils delay.js

100% Statements 6/6
100% Branches 4/4
100% Functions 3/3
100% Lines 6/6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24                        4x 4x 4x 3x   1x         1x  
/**
 * Will return a Promise that will be resolved or rejected after specific time.
 *
 * @category utils
 *
 * @param {number} [timeout=100] - After how many milliseconds the Promise will be resolved
 * @param {any} response - What will be returned as a result in the Promise resolution
 * @param {boolean} [isSuccess=true] - Will the Promise be resolved or rejected
 *
 * @return {object} Promise that will be resolved or rejected after specific time.
 */
function delay(timeout = 100, response, isSuccess = true) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (isSuccess) {
        return resolve(response);
      }
      return reject(response);
    }, timeout);
  });
}
 
module.exports = delay;