pTimeout.js 985 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = pTimeout;
  6. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  7. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  8. var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  9. /**
  10. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  11. *
  12. * This source code is licensed under the MIT license found in the
  13. * LICENSE file in the root directory of this source tree.
  14. */
  15. // A specialized version of `p-timeout` that does not touch globals.
  16. // It does not throw on timeout.
  17. function pTimeout(promise, ms, clearTimeout, setTimeout, onTimeout) {
  18. return new Promise((resolve, reject) => {
  19. const timer = setTimeout(() => resolve(onTimeout()), ms);
  20. promise.then(
  21. val => {
  22. clearTimeout(timer);
  23. resolve(val);
  24. },
  25. err => {
  26. clearTimeout(timer);
  27. reject(err);
  28. }
  29. );
  30. });
  31. }