Skip to content

Instantly share code, notes, and snippets.

@zipang
Created May 7, 2021 14:47
Show Gist options
  • Save zipang/47197f55d76f0004402d048adc353fb6 to your computer and use it in GitHub Desktop.
Save zipang/47197f55d76f0004402d048adc353fb6 to your computer and use it in GitHub Desktop.
/**
* Build a promise that resolve in `ms` milliseconds with the response provided
* NOTE: response may be a function, whose evaluation would be delayed by ms
* @param {Number} ms The delay to wait before resolving the Promise
* @param {Any} response The data to return after ms.
* @return {Promise}
*/
export const delay = (ms, response) =>
new Promise((resolve) =>
setTimeout(
() => resolve(typeof response === "function" ? response() : response),
ms
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment