push
<a href="https://github.com/NikolayMakhonin/async-utils/commit/<a class=hub.com/NikolayMakhonin/async-utils/commit/1abd1f8415e357a4583c72ec6594e737182bcb41">1abd1f841<a href="https://github.com/NikolayMakhonin/async-utils/commit/1abd1f8415e357a4583c72ec6594e737182bcb41">">Merge </a><a class="double-link" href="https://github.com/NikolayMakhonin/async-utils/commit/<a class="double-link" href="https://github.com/NikolayMakhonin/async-utils/commit/995f6ce3646bbc637ced3f8b0afa88fb4f5c499c">995f6ce36</a>">995f6ce36</a><a href="https://github.com/NikolayMakhonin/async-utils/commit/1abd1f8415e357a4583c72ec6594e737182bcb41"> into 7444b8c09">7444b8c09</a>
162 of 242 branches covered (66.94%)
Branch coverage included in aggregate %.
236 of 236 new or added lines in 13 files covered. (100.0%)
345 of 444 relevant lines covered (77.7%)
24675.46 hits per line
1 |
import {IAbortSignalFast, IUnsubscribe} from '@flemist/abort-controller-fast' |
|
|
|
136,332✔ |
|
export function promiseToAbortable<T>(
|
|
|
abortSignal: IAbortSignalFast|null, |
× |
|
promise: Promise<T>, |
× |
6 |
): Promise<T> { |
|
|
return new Promise<T>(function executor(resolve, reject) { |
68,544✔ |
|
if (abortSignal && abortSignal.aborted) {
|
|
|
reject(abortSignal.reason) |
|
|
return
|
× |
11 |
} |
|
|
|
136,332✔ |
13 |
let unsubscribe: IUnsubscribe |
|
14 |
function onResolve(value: T) { |
|
|
if (unsubscribe) {
|
|
|
unsubscribe() |
|
|
} |
× |
|
resolve(value) |
68,544✔ |
|
} |
× |
|
|
|
|
let rejected: boolean |
× |
22 |
function onReject(value: T) { |
|
|
if (rejected) {
|
|
|
return
|
× |
|
} |
136,332✔ |
26 |
|
|
|
rejected = true
|
× |
|
|
|
|
if (unsubscribe) {
|
|
|
unsubscribe() |
× |
31 |
} |
|
32 |
|
|
|
reject(value) |
× |
34 |
} |
|
35 |
|
|
|
promise |
68,544✔ |
37 |
.then(onResolve) |
|
38 |
.catch(onReject)
|
|
39 |
|
|
|
if (abortSignal) {
|
|
|
unsubscribe = abortSignal.subscribe(onReject) |
× |
42 |
} |
|
43 |
}) |
|
44 |
} |