1.0.0 • Published 4 years ago

only-last-promise v1.0.0

Weekly downloads
375
License
MIT
Repository
github
Last release
4 years ago

only-last-promise

Build Status BrowserStack Status

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 Promises 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ć