mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-04 21:19:53 +08:00
65 lines
1.3 KiB
TypeScript
65 lines
1.3 KiB
TypeScript
/// <reference types="node" />
|
|
|
|
import PCancelableCtor = require('p-cancelable');
|
|
import { EventEmitter } from "events";
|
|
|
|
const cancelablePromise: PCancelableCtor.PCancelable<{}> = new PCancelableCtor((onCancel, resolve, reject) => {
|
|
class Worker extends EventEmitter {
|
|
close() {
|
|
}
|
|
}
|
|
|
|
const worker = new Worker();
|
|
|
|
onCancel(() => {
|
|
worker.close();
|
|
});
|
|
|
|
worker.on('finish', resolve);
|
|
worker.on('error', reject);
|
|
});
|
|
|
|
cancelablePromise
|
|
.then(value => {
|
|
console.log('Operation finished successfully:', value);
|
|
})
|
|
.catch(reason => {
|
|
if (cancelablePromise.canceled) {
|
|
// Handle the cancelation here
|
|
console.log('Operation was canceled');
|
|
return;
|
|
}
|
|
|
|
throw reason;
|
|
});
|
|
|
|
setTimeout(() => {
|
|
cancelablePromise.cancel();
|
|
}, 10000);
|
|
|
|
const fn = PCancelableCtor.fn((onCancel: (fn?: () => void) => void, input: string) => {
|
|
const job = {
|
|
start() {
|
|
return Promise.resolve(10);
|
|
},
|
|
cleanup() {
|
|
}
|
|
};
|
|
|
|
onCancel(() => {
|
|
job.cleanup();
|
|
});
|
|
|
|
return job.start();
|
|
});
|
|
|
|
const promise = fn('input');
|
|
let num: number;
|
|
promise.then(innum => num = innum);
|
|
if (!promise.canceled) {
|
|
promise.cancel();
|
|
}
|
|
|
|
const err: PCancelableCtor.CancelError = new PCancelableCtor.CancelError();
|
|
throw err;
|