@common.js/p-timeout 6.1.0 → 6.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  The [p-timeout](https://www.npmjs.com/package/p-timeout) package exported as CommonJS modules.
4
4
 
5
- Exported from [p-timeout@6.1.0](https://www.npmjs.com/package/p-timeout/v/6.1.0) using https://github.com/etienne-martin/common.js.
5
+ Exported from [p-timeout@6.1.2](https://www.npmjs.com/package/p-timeout/v/6.1.2) using https://github.com/etienne-martin/common.js.
package/index.d.ts CHANGED
@@ -3,12 +3,12 @@ export class TimeoutError extends Error {
3
3
  constructor(message?: string);
4
4
  }
5
5
 
6
- export interface ClearablePromise<T> extends Promise<T> {
6
+ export type ClearablePromise<T> = {
7
7
  /**
8
8
  Clear the timeout.
9
9
  */
10
10
  clear: () => void;
11
- }
11
+ } & Promise<T>;
12
12
 
13
13
  export type Options<ReturnType> = {
14
14
  /**
@@ -136,7 +136,7 @@ const delayedPromise = () => setTimeout(200);
136
136
  await pTimeout(delayedPromise(), {
137
137
  milliseconds: 50,
138
138
  fallback: () => {
139
- return pTimeout(delayedPromise(), 300);
139
+ return pTimeout(delayedPromise(), {milliseconds: 300});
140
140
  }
141
141
  });
142
142
  ```
package/index.js CHANGED
@@ -322,14 +322,10 @@ function pTimeout(promise, options) {
322
322
  clearTimeout: clearTimeout
323
323
  } : _customTimers;
324
324
  var timer;
325
- var cancelablePromise = new Promise(function(resolve, reject) {
325
+ var wrappedPromise = new Promise(function(resolve, reject) {
326
326
  if (typeof milliseconds !== "number" || Math.sign(milliseconds) !== 1) {
327
327
  throw new TypeError("Expected `milliseconds` to be a positive number, got `".concat(milliseconds, "`"));
328
328
  }
329
- if (milliseconds === Number.POSITIVE_INFINITY) {
330
- resolve(promise);
331
- return;
332
- }
333
329
  if (options.signal) {
334
330
  var signal = options.signal;
335
331
  if (signal.aborted) {
@@ -339,6 +335,12 @@ function pTimeout(promise, options) {
339
335
  reject(getAbortedReason(signal));
340
336
  });
341
337
  }
338
+ if (milliseconds === Number.POSITIVE_INFINITY) {
339
+ promise.then(resolve, reject);
340
+ return;
341
+ }
342
+ // We create the error outside of `setTimeout` to preserve the stack trace.
343
+ var timeoutError = new TimeoutError();
342
344
  timer = customTimers.setTimeout.call(undefined, function() {
343
345
  if (fallback) {
344
346
  try {
@@ -356,8 +358,8 @@ function pTimeout(promise, options) {
356
358
  } else if (_instanceof(message, Error)) {
357
359
  reject(message);
358
360
  } else {
359
- var errorMessage = message !== null && message !== void 0 ? message : "Promise timed out after ".concat(milliseconds, " milliseconds");
360
- reject(new TimeoutError(errorMessage));
361
+ timeoutError.message = message !== null && message !== void 0 ? message : "Promise timed out after ".concat(milliseconds, " milliseconds");
362
+ reject(timeoutError);
361
363
  }
362
364
  }, milliseconds);
363
365
  _asyncToGenerator(function() {
@@ -368,8 +370,8 @@ function pTimeout(promise, options) {
368
370
  _state.trys.push([
369
371
  0,
370
372
  2,
371
- 3,
372
- 4
373
+ ,
374
+ 3
373
375
  ]);
374
376
  return [
375
377
  4,
@@ -381,21 +383,16 @@ function pTimeout(promise, options) {
381
383
  ]);
382
384
  return [
383
385
  3,
384
- 4
386
+ 3
385
387
  ];
386
388
  case 2:
387
389
  error = _state.sent();
388
390
  reject(error);
389
391
  return [
390
392
  3,
391
- 4
393
+ 3
392
394
  ];
393
395
  case 3:
394
- customTimers.clearTimeout.call(undefined, timer);
395
- return [
396
- 7
397
- ];
398
- case 4:
399
396
  return [
400
397
  2
401
398
  ];
@@ -403,6 +400,9 @@ function pTimeout(promise, options) {
403
400
  });
404
401
  })();
405
402
  });
403
+ var cancelablePromise = wrappedPromise.finally(function() {
404
+ cancelablePromise.clear();
405
+ });
406
406
  cancelablePromise.clear = function() {
407
407
  customTimers.clearTimeout.call(undefined, timer);
408
408
  timer = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@common.js/p-timeout",
3
- "version": "6.1.0",
3
+ "version": "6.1.2",
4
4
  "description": "p-timeout package exported as CommonJS modules",
5
5
  "license": "MIT",
6
6
  "repository": "etienne-martin/common.js",
@@ -24,7 +24,7 @@
24
24
  "p-cancelable": "^4.0.1",
25
25
  "time-span": "^5.1.0",
26
26
  "tsd": "^0.22.0",
27
- "xo": "^0.51.0"
27
+ "xo": "^0.54.2"
28
28
  },
29
29
  "homepage": "https://github.com/etienne-martin/common.js#readme",
30
30
  "dependencies": {},