only-last-promise
v1.0.0
Published
Resolve or reject only last Promise.
Downloads
3,543
Maintainers
Readme
only-last-promise
Resolve or reject only last Promise.
Useful if you want to "abort" already running async operations started with debounced input event.
Install
npm install only-last-promise --save
Usage
When calling the wrapper function multiple times, only the last returned
Promise
will resolve or reject and all other Promise
s will be aborted with
DiscardSignal
error.
import onlyLastPromise, { DiscardSignal } from 'only-last-promise';
const wrapper = onlyLastPromise();
async function main() {
try {
return await wrapper(fetch('/becky'));
} catch (error) {
if (!(error instanceof DiscardSignal)) {
throw error;
}
}
}
API
onlyLastPromise()
Returns: Function
Factory function which returns wrapper function.
wrapper(promise)
Type: Function
promise
Type: Promise
Promise
to handle.
Browser support
Tested in IE9+ and all modern browsers.
Test
For automated tests, run npm run test:automated
(append :watch
for watcher
support).
License
MIT © Ivan Nikolić