介绍
reduce-right-async
是一个能够以异步方式执行 JavaScript 中 Array.prototype.reduceRight()
方法的 npm 包。
安装
可以通过 npm 进行安装,并在项目中引入该库:
npm i reduce-right-async
使用
reduce-right-async
的使用与 Array.prototype.reduceRight()
类似,但有些异步的差异。
reduceRightAsync()
异步函数 reduceRightAsync()
接收三个参数:array
,asyncCallback
和 initialValue
。
-- -------------------- ---- ------- ----- ---------------- - ------------------------------ ----- ----- - ---- --- ---- ----- ------------- - ----- --------------- ------------- ------------- ------ -- - ------ ------------- - ------------- -- ----- ------------ - -- ----- ------ - ----- ----------------------- -------------- -------------- -------------------- -- ------- --
其中:
array
: 需要进行操作的数组.asyncCallback
: 执行操作的异步函数.initialValue
(可选): 初始值,如果不提供则默认为数组的最后一个元素.
asyncCallback
异步函数的参数:
previousValue
: reduceRightAsync 函数返回的最后一个值 (或者是提供的初始值).currentValue
: 当前进行操作的元素.currentIndex
(可选):currentValue
元素的下标.array
: 参与操作的数组.
异步函数 asyncCallback
必须返回一个 promise 对象,否则将会导致程序出错。
reduceRightAsyncSeries()
reduceRightAsyncSeries()
与 reduceRightAsync()
类似,不同的是 reduceRightAsyncSeries()
会依次地执行异步函数并且保证顺序。
-- -------------------- ---- ------- ----- ---------------------- - ------------------------------------- ----- ----- - --- -- --- ----- ------------- - ----- --------------- ------------- -- - ------ --- --------------- -- - ------------- -- - --------------------- - -------------- -- ------------ - ----- --- -- ----- ------------ - -- ----- ------ - ----- ----------------------------- -------------- -------------- -------------------- -- ------- -
reduceRightAsyncLimit()
reduceRightAsyncLimit()
方法与 reduceRightAsync()
类似,不过有一个限制,即异步函数 asyncCallback
最多可以同时运行 limit
个。
-- -------------------- ---- ------- ----- --------------------- - ------------------------------------ ----- ----- - --- -- --- ----- ------------- - ----- --------------- ------------- -- - ------ --- --------------- -- - ------------- -- - --------------------- - -------------- -- ------------ - ----- --- -- ----- ------------ - -- ----- ----- - -- ----- ------ - ----- ---------------------------- -------------- ------------- ------- -------------------- -- ------- -
总结
以上是 reduce-right-async
的简单教程,通过这个包可以执行一些有用的异步函数操作。希望这篇文章有帮助!
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/60067008e361a36e0bce8b39