5 ms·
Quick question - would something like this cover the basic cancelable promise use case, or am I missing something important about what LazyPromise does differen
by guntis_dev 9mo ago
Quick question - would something like this cover the basic cancelable promise use case, or am I missing something important about what LazyPromise does differently?
function cancelablePromise() {
const promise = new Promise(resolve => setTimeout(resolve, 1000))
let cancel: () => void
const cancelPromise = new Promise((_, reject) => {
cancel = () => reject("promise canceled")
})
const wrappedPromise = Promise.race([promise, cancelPromise])
return {
promise: wrappedPromise,
cancel: cancel,
}
}
- ivan7237d 9mo agoYeah, I think this will cover the basic use-case, although the way I'd approach this is by starting with AbortSignal API and then maybe creating a wrapper around it. In a way `eager` and `lazy` functions from LazyPromise library are such wrappers.