4 ms·
It's not that difficult to extend Promises. Show some code that's easy to follow. If cancelled/canceled is too confusing, there are synonyms. function long
by numtel 10y ago
It's not that difficult to extend Promises. Show some code that's easy to follow. If cancelled/canceled is too confusing, there are synonyms.
function longTask() {
return new AbortablePromise((resolve, reject, onAbort) => {
const timer = setTimeout(resolve, 1000);
onAbort(reason => clearTimeout(timer));
});
}
const myTask = longTask
.aborted(reason => console.log('Aborted'))
.then(result => console.log('Done'))
.catch(reason => console.log('Error'));
setTimeout(() => myTask.abort('Changed my mind'), 300);
I did something similar with reporting progress: https://github.com/numtel/progress-promise https://github.com/numtel/progress-promise