mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 06:47:03 +00:00
Merge pull request #28336 from kendraknittel/patch-1
[p-cancelable] Make types compatible with 0.5
This commit is contained in:
Vendored
+6
-5
@@ -1,4 +1,4 @@
|
||||
// Type definitions for p-cancelable 0.3
|
||||
// Type definitions for p-cancelable 0.5
|
||||
// Project: https://github.com/sindresorhus/p-cancelable#readme
|
||||
// Definitions by: BendingBender <https://github.com/BendingBender>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
@@ -11,20 +11,21 @@ interface PCancelableConstructor extends PromiseConstructor {
|
||||
readonly prototype: PCancelable.PCancelable<any>;
|
||||
readonly CancelError: PCancelable.CancelErrorConstructor;
|
||||
fn<T, R>(wrapper: (onCancel: (fn?: () => void) => void, input: T) => PromiseLike<R>): (input: T) => PCancelable.PCancelable<R>;
|
||||
new<T>(executor: (onCancel: (fn?: () => void) => void, resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): PCancelable.PCancelable<T>;
|
||||
new<T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void, onCancel: (fn?: () => void) => void) => void): PCancelable.PCancelable<T>;
|
||||
}
|
||||
|
||||
declare namespace PCancelable {
|
||||
interface PCancelable<T> extends Promise<T> {
|
||||
readonly canceled: boolean;
|
||||
cancel(): void;
|
||||
readonly isCanceled: boolean;
|
||||
cancel(reason?: string): void;
|
||||
}
|
||||
|
||||
interface CancelErrorConstructor extends ErrorConstructor {
|
||||
new (): CancelError;
|
||||
new (reason?: string): CancelError;
|
||||
}
|
||||
|
||||
interface CancelError extends Error {
|
||||
readonly name: 'CancelError';
|
||||
readonly isCanceled: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import PCancelableCtor = require('p-cancelable');
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
const cancelablePromise: PCancelableCtor.PCancelable<{}> = new PCancelableCtor((onCancel, resolve, reject) => {
|
||||
const cancelablePromise: PCancelableCtor.PCancelable<{}> = new PCancelableCtor((resolve, reject, onCancel) => {
|
||||
class Worker extends EventEmitter {
|
||||
close() {
|
||||
}
|
||||
@@ -24,7 +24,7 @@ cancelablePromise
|
||||
console.log('Operation finished successfully:', value);
|
||||
})
|
||||
.catch(reason => {
|
||||
if (cancelablePromise.canceled) {
|
||||
if (cancelablePromise.isCanceled) {
|
||||
// Handle the cancelation here
|
||||
console.log('Operation was canceled');
|
||||
return;
|
||||
@@ -56,7 +56,7 @@ const fn = PCancelableCtor.fn((onCancel: (fn?: () => void) => void, input: strin
|
||||
const promise = fn('input');
|
||||
let num: number;
|
||||
promise.then(innum => num = innum);
|
||||
if (!promise.canceled) {
|
||||
if (!promise.isCanceled) {
|
||||
promise.cancel();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user