内容简介: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ć
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
首席产品官2 从白领到金领
车马 / 机械工业出版社 / 79元
《首席产品官》共2册,旨在为产品新人成长为产品行家,产品白领成长为产品金领,最后成长为首席产品官(CPO)提供产品认知、能力体系、成长方法三个维度的全方位指导。 作者在互联网领域从业近20年,是中国早期的互联网产品经理,曾是周鸿祎旗下“3721”的产品经理,担任CPO和CEO多年。作者将自己多年来的产品经验体系化,锤炼出了“产品人的能力杠铃模型”(简称“杠铃模型”),简洁、直观、兼容性好、实......一起来看看 《首席产品官2 从白领到金领》 这本书的介绍吧!