pTimeout.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = pTimeout;
  6. var global = (function () {
  7. if (typeof globalThis !== 'undefined') {
  8. return globalThis;
  9. } else if (typeof global !== 'undefined') {
  10. return global;
  11. } else if (typeof self !== 'undefined') {
  12. return self;
  13. } else if (typeof window !== 'undefined') {
  14. return window;
  15. } else {
  16. return Function('return this')();
  17. }
  18. })();
  19. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  20. var global = (function () {
  21. if (typeof globalThis !== 'undefined') {
  22. return globalThis;
  23. } else if (typeof global !== 'undefined') {
  24. return global;
  25. } else if (typeof self !== 'undefined') {
  26. return self;
  27. } else if (typeof window !== 'undefined') {
  28. return window;
  29. } else {
  30. return Function('return this')();
  31. }
  32. })();
  33. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  34. var global = (function () {
  35. if (typeof globalThis !== 'undefined') {
  36. return globalThis;
  37. } else if (typeof global !== 'undefined') {
  38. return global;
  39. } else if (typeof self !== 'undefined') {
  40. return self;
  41. } else if (typeof window !== 'undefined') {
  42. return window;
  43. } else {
  44. return Function('return this')();
  45. }
  46. })();
  47. var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  48. /**
  49. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  50. *
  51. * This source code is licensed under the MIT license found in the
  52. * LICENSE file in the root directory of this source tree.
  53. */
  54. // A specialized version of `p-timeout` that does not touch globals.
  55. // It does not throw on timeout.
  56. function pTimeout(promise, ms, clearTimeout, setTimeout, onTimeout) {
  57. return new Promise((resolve, reject) => {
  58. const timer = setTimeout(() => resolve(onTimeout()), ms);
  59. promise.then(
  60. val => {
  61. clearTimeout(timer);
  62. resolve(val);
  63. },
  64. err => {
  65. clearTimeout(timer);
  66. reject(err);
  67. }
  68. );
  69. });
  70. }