real-cancellable-promise
Advanced tools
Comparing version 1.0.0-alpha.5 to 1.0.0-alpha.6
@@ -10,3 +10,3 @@ # Contributing to real-cancellable-promise | ||
3. Run tests: `yarn test` | ||
4. Run ESLint: `yarn lint '**/*.ts?(x)'` | ||
4. Run ESLint: `yarn lint-all` | ||
@@ -13,0 +13,0 @@ ## TypeDoc |
@@ -16,5 +16,17 @@ /** | ||
* object. | ||
* | ||
* @typeParam T what the `CancellablePromise` resolves to | ||
*/ | ||
export declare class CancellablePromise<T> { | ||
/** | ||
* As a consumer of the library, you shouldn't ever need to access | ||
* `CancellablePromise.promise` directly. | ||
* | ||
* If you are subclassing `CancellablePromise` for some reason, you | ||
* can access this property. | ||
*/ | ||
protected readonly promise: Promise<T>; | ||
/** | ||
* Cancel the `CancellablePromise`. | ||
*/ | ||
readonly cancel: (reason?: string) => void; | ||
@@ -82,73 +94,150 @@ /** | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1>(promises: [CancellablePromise<T1>]): CancellablePromise<[T1]>; | ||
static all<T1, T2>(promises: [CancellablePromise<T1>, CancellablePromise<T2>]): CancellablePromise<[T1, T2]>; | ||
static all<T1, T2, T3>(promises: [CancellablePromise<T1>, CancellablePromise<T2>, CancellablePromise<T3>]): CancellablePromise<[T1, T2, T3]>; | ||
static all<T1, T2, T3, T4>(promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4> | ||
]): CancellablePromise<[T1, T2, T3, T4]>; | ||
static all<T1, T2, T3, T4, T5>(promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5]>; | ||
static all<T1, T2, T3, T4, T5, T6>(promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5>, | ||
CancellablePromise<T6> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5, T6]>; | ||
static all<T1, T2, T3, T4, T5, T6, T7>(promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5>, | ||
CancellablePromise<T6>, | ||
CancellablePromise<T7> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7]>; | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8>(promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5>, | ||
CancellablePromise<T6>, | ||
CancellablePromise<T7>, | ||
CancellablePromise<T8> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8]>; | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5>, | ||
CancellablePromise<T6>, | ||
CancellablePromise<T7>, | ||
CancellablePromise<T8>, | ||
CancellablePromise<T9> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5>, | ||
CancellablePromise<T6>, | ||
CancellablePromise<T7>, | ||
CancellablePromise<T8>, | ||
CancellablePromise<T9>, | ||
CancellablePromise<T10> | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5>, | ||
T6 | PromiseLike<T6>, | ||
T7 | PromiseLike<T7>, | ||
T8 | PromiseLike<T8>, | ||
T9 | PromiseLike<T9>, | ||
T10 | PromiseLike<T10> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; | ||
static all<T>(promises: CancellablePromise<T>[]): CancellablePromise<T[]>; | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5>, | ||
T6 | PromiseLike<T6>, | ||
T7 | PromiseLike<T7>, | ||
T8 | PromiseLike<T8>, | ||
T9 | PromiseLike<T9> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8>(values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5>, | ||
T6 | PromiseLike<T6>, | ||
T7 | PromiseLike<T7>, | ||
T8 | PromiseLike<T8> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8]>; | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4, T5, T6, T7>(values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5>, | ||
T6 | PromiseLike<T6>, | ||
T7 | PromiseLike<T7> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7]>; | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4, T5, T6>(values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5>, | ||
T6 | PromiseLike<T6> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5, T6]>; | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4, T5>(values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5> | ||
]): CancellablePromise<[T1, T2, T3, T4, T5]>; | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4>(values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4> | ||
]): CancellablePromise<[T1, T2, T3, T4]>; | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3>(values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3> | ||
]): CancellablePromise<[T1, T2, T3]>; | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): CancellablePromise<[T1, T2]>; | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T>(values: readonly (T | PromiseLike<T>)[]): CancellablePromise<T[]>; | ||
/** | ||
* Creates a `CancellablePromise` that is resolved with an array of results | ||
@@ -164,5 +253,7 @@ * when all of the provided `Promises` resolve or reject. | ||
* Creates a `CancellablePromise` that is resolved with an array of results | ||
* when all of the provided `Promises` resolve or reject. | ||
* @param values An array of `Promises`. | ||
* @returns A new `CancellablePromise`. | ||
* when all of the provided `Promise`s resolve or reject. | ||
* | ||
* @param values An array of `Promise`s. | ||
* @returns A new `CancellablePromise`. Canceling it cancels all of the input | ||
* promises. | ||
*/ | ||
@@ -174,3 +265,4 @@ static allSettled<T>(values: Iterable<T>): CancellablePromise<PromiseSettledResult<T extends PromiseLike<infer U> ? U : T>[]>; | ||
* @param values An array of `Promises`. | ||
* @returns A new `CancellablePromise`. | ||
* @returns A new `CancellablePromise`. Canceling it cancels all of the input | ||
* promises. | ||
*/ | ||
@@ -177,0 +269,0 @@ static race<T>(values: readonly T[]): CancellablePromise<T extends PromiseLike<infer U> ? U : T>; |
@@ -5,3 +5,3 @@ "use strict"; | ||
const Cancellation_1 = require("./Cancellation"); | ||
const Internal_1 = require("./Internal"); | ||
const noop_1 = require("./noop"); | ||
/** | ||
@@ -21,2 +21,4 @@ * Determines if an arbitrary value is a thenable with a cancel method. | ||
* object. | ||
* | ||
* @typeParam T what the `CancellablePromise` resolves to | ||
*/ | ||
@@ -98,3 +100,3 @@ class CancellablePromise { | ||
static resolve(value) { | ||
return new CancellablePromise(Promise.resolve(value), Internal_1.noop); | ||
return new CancellablePromise(Promise.resolve(value), noop_1.noop); | ||
} | ||
@@ -110,8 +112,17 @@ /** | ||
static reject(reason) { | ||
return new CancellablePromise(Promise.reject(reason), Internal_1.noop); | ||
return new CancellablePromise(Promise.reject(reason), noop_1.noop); | ||
} | ||
static all(promises) { | ||
return new CancellablePromise(Promise.all(promises), () => { | ||
for (const p of promises) { | ||
p.cancel(); | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all(values) { | ||
return new CancellablePromise(Promise.all(values), () => { | ||
for (const value of values) { | ||
if (isPromiseWithCancel(value)) | ||
value.cancel(); | ||
} | ||
@@ -134,3 +145,4 @@ }); | ||
* @param values An array of `Promises`. | ||
* @returns A new `CancellablePromise`. | ||
* @returns A new `CancellablePromise`. Canceling it cancels all of the input | ||
* promises. | ||
*/ | ||
@@ -165,5 +177,8 @@ static race(values) { | ||
let timer; | ||
let rejectFn = Internal_1.noop; | ||
let rejectFn = noop_1.noop; | ||
const promise = new Promise((resolve, reject) => { | ||
timer = setTimeout(resolve, ms); | ||
timer = setTimeout(() => { | ||
resolve(); | ||
rejectFn = noop_1.noop; | ||
}, ms); | ||
rejectFn = reject; | ||
@@ -170,0 +185,0 @@ }); |
export * from './Cancellation'; | ||
export * from './CancellablePromise'; | ||
export * from './Utils'; | ||
export * from './utils'; |
@@ -15,2 +15,2 @@ "use strict"; | ||
__exportStar(require("./CancellablePromise"), exports); | ||
__exportStar(require("./Utils"), exports); | ||
__exportStar(require("./utils"), exports); |
{ | ||
"name": "real-cancellable-promise", | ||
"version": "1.0.0-alpha.5", | ||
"version": "1.0.0-alpha.6", | ||
"description": "A simple cancellable promise implementation that cancels the underlying HTTP call.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -118,6 +118,4 @@ # real-cancellable-promise | ||
const promise = xhr.catch((e) => { | ||
const thrownXhr = e as JQuery.jqXHR | ||
if (e.statusText === 'abort') throw new Cancellation() | ||
if (thrownXhr.statusText === 'abort') throw new Cancellation() | ||
// rethrow the original error | ||
@@ -140,3 +138,3 @@ throw e | ||
`CancellablePromise` supports all the methods that the normal `Promise` object | ||
supports, with the exception of `Promise.any` (ES2021). See the [API | ||
supports, except `Promise.any` (ES2021). See the [API | ||
Reference](https://srmagura.github.io/real-cancellable-promise) for details. | ||
@@ -358,3 +356,3 @@ | ||
**Node.js:** 14+. `AbortController` is only available in Node 15+. `real-cancellable-promise` ships as an ES module, so you need to use an ES-style `import` statement rather than `require()`. | ||
**Node.js:** 14+. `AbortController` is only available in Node 15+. | ||
@@ -361,0 +359,0 @@ # License |
@@ -8,3 +8,2 @@ // Jest bug: https://github.com/facebook/jest/issues/11876 | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
jest.useFakeTimers() | ||
@@ -234,3 +233,3 @@ }) | ||
const x0: [0] = await CancellablePromise.all([p0]) | ||
const x0: 0[] = await CancellablePromise.all([p0]) | ||
const x2: [0, 1, 2] = await CancellablePromise.all([p0, p1, p2]) | ||
@@ -292,2 +291,14 @@ const x3: [0, 1, 2, 3] = await CancellablePromise.all([p0, p1, p2, p3]) | ||
it('supports normal promises, thenables, and non-promises', async () => { | ||
const [r0, r1, r2] = await CancellablePromise.all([ | ||
Promise.resolve(0), | ||
Promise.resolve(1) as PromiseLike<number>, | ||
2, | ||
]) | ||
expect(r0).toBe(0) | ||
expect(r1).toBe(1) | ||
expect(r2).toBe(2) | ||
}) | ||
it('returns the results of the input promises', async () => { | ||
@@ -294,0 +305,0 @@ const p0 = getPromise<0>(0) |
import { Cancellation } from './Cancellation' | ||
import { noop } from './Internal' | ||
import { noop } from './noop' | ||
@@ -25,8 +25,22 @@ /** | ||
* object. | ||
* | ||
* @typeParam T what the `CancellablePromise` resolves to | ||
*/ | ||
export class CancellablePromise<T> { | ||
/** | ||
* As a consumer of the library, you shouldn't ever need to access | ||
* `CancellablePromise.promise` directly. | ||
* | ||
* If you are subclassing `CancellablePromise` for some reason, you | ||
* can access this property. | ||
*/ | ||
protected readonly promise: Promise<T> | ||
// IMPORTANT: When defining a new `cancel` function, e.g. in the implementation of `then`, | ||
// IMPORTANT: When defining a new `cancel` function, | ||
// e.g. in the implementation of `then`, | ||
// always use an arrow function so that `this` is bound. | ||
/** | ||
* Cancel the `CancellablePromise`. | ||
*/ | ||
readonly cancel: (reason?: string) => void | ||
@@ -156,105 +170,189 @@ | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1>(promises: [CancellablePromise<T1>]): CancellablePromise<[T1]> | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>( | ||
values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5>, | ||
T6 | PromiseLike<T6>, | ||
T7 | PromiseLike<T7>, | ||
T8 | PromiseLike<T8>, | ||
T9 | PromiseLike<T9>, | ||
T10 | PromiseLike<T10> | ||
] | ||
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]> | ||
static all<T1, T2>( | ||
promises: [CancellablePromise<T1>, CancellablePromise<T2>] | ||
): CancellablePromise<[T1, T2]> | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9>( | ||
values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5>, | ||
T6 | PromiseLike<T6>, | ||
T7 | PromiseLike<T7>, | ||
T8 | PromiseLike<T8>, | ||
T9 | PromiseLike<T9> | ||
] | ||
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]> | ||
static all<T1, T2, T3>( | ||
promises: [CancellablePromise<T1>, CancellablePromise<T2>, CancellablePromise<T3>] | ||
): CancellablePromise<[T1, T2, T3]> | ||
static all<T1, T2, T3, T4>( | ||
promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4> | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8>( | ||
values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5>, | ||
T6 | PromiseLike<T6>, | ||
T7 | PromiseLike<T7>, | ||
T8 | PromiseLike<T8> | ||
] | ||
): CancellablePromise<[T1, T2, T3, T4]> | ||
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8]> | ||
static all<T1, T2, T3, T4, T5>( | ||
promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5> | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4, T5, T6, T7>( | ||
values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5>, | ||
T6 | PromiseLike<T6>, | ||
T7 | PromiseLike<T7> | ||
] | ||
): CancellablePromise<[T1, T2, T3, T4, T5]> | ||
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7]> | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4, T5, T6>( | ||
promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5>, | ||
CancellablePromise<T6> | ||
values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5>, | ||
T6 | PromiseLike<T6> | ||
] | ||
): CancellablePromise<[T1, T2, T3, T4, T5, T6]> | ||
static all<T1, T2, T3, T4, T5, T6, T7>( | ||
promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5>, | ||
CancellablePromise<T6>, | ||
CancellablePromise<T7> | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4, T5>( | ||
values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4>, | ||
T5 | PromiseLike<T5> | ||
] | ||
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7]> | ||
): CancellablePromise<[T1, T2, T3, T4, T5]> | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8>( | ||
promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5>, | ||
CancellablePromise<T6>, | ||
CancellablePromise<T7>, | ||
CancellablePromise<T8> | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3, T4>( | ||
values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3>, | ||
T4 | PromiseLike<T4> | ||
] | ||
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8]> | ||
): CancellablePromise<[T1, T2, T3, T4]> | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9>( | ||
promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5>, | ||
CancellablePromise<T6>, | ||
CancellablePromise<T7>, | ||
CancellablePromise<T8>, | ||
CancellablePromise<T9> | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2, T3>( | ||
values: readonly [ | ||
T1 | PromiseLike<T1>, | ||
T2 | PromiseLike<T2>, | ||
T3 | PromiseLike<T3> | ||
] | ||
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]> | ||
): CancellablePromise<[T1, T2, T3]> | ||
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>( | ||
promises: [ | ||
CancellablePromise<T1>, | ||
CancellablePromise<T2>, | ||
CancellablePromise<T3>, | ||
CancellablePromise<T4>, | ||
CancellablePromise<T5>, | ||
CancellablePromise<T6>, | ||
CancellablePromise<T7>, | ||
CancellablePromise<T8>, | ||
CancellablePromise<T9>, | ||
CancellablePromise<T10> | ||
] | ||
): CancellablePromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]> | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T1, T2>( | ||
values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>] | ||
): CancellablePromise<[T1, T2]> | ||
static all<T>(promises: CancellablePromise<T>[]): CancellablePromise<T[]> | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all<T>(values: readonly (T | PromiseLike<T>)[]): CancellablePromise<T[]> | ||
static all(promises: CancellablePromise<unknown>[]): CancellablePromise<unknown> { | ||
return new CancellablePromise(Promise.all(promises), () => { | ||
for (const p of promises) { | ||
p.cancel() | ||
/** | ||
* Analogous to `Promise.all`. | ||
* | ||
* @param values an array that may contain `CancellablePromise`s, promises, | ||
* thenables, and resolved values | ||
* @returns a [[`CancellablePromise`]], which, if canceled, will cancel each | ||
* of the promises passed in to `CancellablePromise.all`. | ||
*/ | ||
static all(values: readonly unknown[]): CancellablePromise<unknown> { | ||
return new CancellablePromise(Promise.all(values), () => { | ||
for (const value of values) { | ||
if (isPromiseWithCancel(value)) value.cancel() | ||
} | ||
@@ -280,5 +378,7 @@ }) | ||
* Creates a `CancellablePromise` that is resolved with an array of results | ||
* when all of the provided `Promises` resolve or reject. | ||
* @param values An array of `Promises`. | ||
* @returns A new `CancellablePromise`. | ||
* when all of the provided `Promise`s resolve or reject. | ||
* | ||
* @param values An array of `Promise`s. | ||
* @returns A new `CancellablePromise`. Canceling it cancels all of the input | ||
* promises. | ||
*/ | ||
@@ -305,3 +405,4 @@ static allSettled<T>( | ||
* @param values An array of `Promises`. | ||
* @returns A new `CancellablePromise`. | ||
* @returns A new `CancellablePromise`. Canceling it cancels all of the input | ||
* promises. | ||
*/ | ||
@@ -344,3 +445,6 @@ static race<T>( | ||
const promise = new Promise<void>((resolve, reject) => { | ||
timer = setTimeout(resolve, ms) | ||
timer = setTimeout(() => { | ||
resolve() | ||
rejectFn = noop | ||
}, ms) | ||
rejectFn = reject | ||
@@ -347,0 +451,0 @@ }) |
export * from './Cancellation' | ||
export * from './CancellablePromise' | ||
export * from './Utils' | ||
export * from './utils' |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74180
1601
363