Bamieh/reflow

View on GitHub
packages/reflow-core/lib/utils/resolve-before.js

Summary

Maintainability
A
0 mins
Test Coverage
/**
 * Creates a promise that resolves at the specified number of ms.
 *
 * @param ms
 * @returns {Promise}
 */
const PROMISE_TIMEOUT = {};
const transmission = require('winston-transmission')

function resolveBefore(promise, ms, msg) {
  return Promise.race([
    promise,
    new Promise(resolve => setTimeout(() => resolve(PROMISE_TIMEOUT), ms)),
  ]).then((res) => {
    if (res === PROMISE_TIMEOUT) transmission.info(msg, { timeout: ms });
    return res;
  });
}

module.exports.resolveBefore = resolveBefore