sleep.js 588 B

123456789101112131415161718192021
  1. "use strict";
  2. var isValue = require("es5-ext/object/is-value")
  3. , isPromise = require("es5-ext/object/is-promise")
  4. , nextTick = require("next-tick")
  5. , ensureTimeout = require("../valid-timeout");
  6. module.exports = function (/* timeout */) {
  7. var Constructor = isPromise(this) ? this.constructor : Promise;
  8. var timeout = arguments[0];
  9. if (isValue(timeout)) timeout = ensureTimeout(timeout);
  10. return new Constructor(function (resolve) {
  11. if (isValue(timeout)) {
  12. setTimeout(function () {
  13. resolve();
  14. }, timeout);
  15. } else {
  16. nextTick(resolve);
  17. }
  18. });
  19. };