only-last-promise - Resolve or reject only last Promise

栏目: IT技术 · 发布时间: 4年前

内容简介:Resolve or reject only last Promise.Useful if you want to "abort" already running async operations started with debounced input event.When calling the wrapper function multiple times, only the last returned

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.

In the following example, fetch requests for /buddy and /allie will be discarded (they will return undefined ), and only /becky will be resolved with Response .

import onlyLastPromise, { DiscardSignal } from 'only-last-promise';

const wrapper = onlyLastPromise();

const wrappedFetch = async (url) => {
	try {
		return await wrapper(fetch(url));
	} catch (error) {
		if (!(error instanceof DiscardSignal)) {
			throw error;
		}
	}
};

(async () => {
	await Promise.all([
		wrappedFetch('/buddy'),
		wrappedFetch('/allie'),
		wrappedFetch('/becky')
	]);
	// => [undefined, undefined, Response]
})();

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, assuming Promise is available.

Test

For automated tests, run npm run test:automated (append :watch for watcher support).

License

MIT © Ivan Nikolić


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

响应式Web设计实践

响应式Web设计实践

[美] Tim Kadlec / 侯鸿儒 / 人民邮电出版社 / 2013-3-1 / 55.00元

随着各种各样的移动设备不断地涌现到使用者面前,Web设计的适应性已经成为设计师们所面临的最为艰巨的挑战。你设计出的网站不仅要在桌面计算机的大尺寸屏幕上可以为用户提供友好的UI和用户体验,同时在小尺寸屏幕上也应该可以提供一致的用户体验,并可以让用户能够在桌面大屏幕上和移动小屏幕上平滑切换,同时没有任何的不适应感觉。 本书作者是一位出色的开发者,在本书中,他将诸多技术和设计理念杂糅在一起,再辅以......一起来看看 《响应式Web设计实践》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

html转js在线工具
html转js在线工具

html转js在线工具