Douglas Crockford 大神写的 JavaScript 异步控制库:RQ(下)

RQ 库 The RQ Library

RQ 仅仅是一个文件,rq.js。运行起来创建一个 RQ 变量,该变量包含四个函数。

RQ is delivered as a single file, rq.js.When run, it produces an RQ variable containing an objectcontaining four functions.

RQ.sequence(requestors)RQ.sequence(requestors, milliseconds)

RQ.sequence有一个请求者函数所构成的数组的参数,还有一个可选的参数,用于表示超时的。返回值是另一个总的请求者,它能够依次执行里面的每一个请求者,把上一个结果按照顺序地交给下一个。如果全体完成,那么最后一个请求者的结果将是全体的结果,以数组形式出现。如果数组中的一个请求者失败了,那么表示这个串行队列失败。数组并没有改变。

RQ.sequence takes an array of requestor functions and anoptional time limit in milliseconds. It returns a requestor function thatwill start each of the requestors in order, giving the result of each tothe next one. If all complete successfully, the result will be the resultof the last requestor in the array. If any of the requestors in the arrayfail, then the sequence fails. The array is not modified.

如果传入毫秒数的参数,则表示是超时的参数。

If a milliseconds argument is provided, then the sequence will fail if itdoes not finish before the time limit.

RQ.parallel(requireds)RQ.parallel(requireds, milliseconds)RQ.parallel(requireds, optionals)RQ.parallel(requireds, milliseconds, optionals)RQ.parallel(requireds, optionals, untilliseconds)RQ.parallel(requireds, milliseconds, optionals, untilliseconds)

RQ.parallel 有一个必要请求者参数,为请求者函数所构成数组,及其用于表示超时的毫秒数参数(可选的),另外还有一个可选请求者参数(也是数组),及其用于表示超时的毫秒数参数(可选的)。该函数会返回另外一个总的请求者,会立刻执行必要请求者和可选请求者。结果为所有请求者的结果。如果有两个数组传入,结果数组的长度是那个传入数组总数之和。第一个请求者的结果就是结果数组中的第一个元素。整个并行任务完成与否取决于那些必要请求者,必要请求者成功了则成功。可选请求者的成功与否不会影响整个请求任务。这可用于最佳的尝试,从而获得了可以达成的结果。数组并没有改变。

RQ.parallel takes an array of required requestor functions,and an optional time limit in milliseconds, and an optional array ofoptional requestors and an optional guaranteed time for the optionalrequestors. It returns a requestor function that will start all of therequired and optional requestors at once. The result is an arraycontaining all of the results from all of the requestors. If both arrayswere provided, the length of the results array is the sum of the lengthsof the two arrays. The result of the first requestor will be in the firstposition of the results array. The parallel will succeed only if all ofthe required requestors succeed. The array of optional requestors containsrequests that can fail without causing the entire parallel operation tofail. It can be used as a best effort, obtaining the results that areattainable. The arrays are not modified.

如果提供了毫秒数的参数,那么表示在时限到来之前还没有完成请求的话,将宣告并行工作失败。缺省情况下(没有提供 untilliseconds),不管可选请求者怎么样,必要请求者完毕了可选请求者就完毕。如果提供了 untilliseconds 参数,就按照这个时限来对可选请求者限制。如果没有提供必要请求者,那么应该至少要传入一个可选请求者,并且在规定时限完成的话,这个并行就成功了。

If a milliseconds argument is provided, then the parallel will fail ifall of the required requestors do not finish before the time limit. Bydefault, the optionals have until all of the required requestors finish.The untilliseconds argument guarantees the optionals some amount of time.untilliseconds may not be larger than milliseconds. If the requireds arrayis empty, and if at least one optional requestor is successful within theallotted time, then the parallel succeeds.

RQ.parallel 并没有对 JavaScript语言层面添加并行机制。它能让 JavaScript 程序充分利用语言层面原生的并行机制。程序自己并不是一脚包办所有事情,而且发出请求来让其他的进程或者机器来搞定事情,这些进程或者机器都是独立执行的。

RQ.parallel does not add parallelism to JavaScript. Itallows JavaScript programs to effectively exploit the inherent parallelismof the universe. It is likely that many of the requestors will becommunicating with other processes and other machines. Those otherprocesses and machines will be executing independently.

RQ.race(requestors)RQ.race(requestors, milliseconds)

RQ.race 有一个请求者函数所构成的数组的参数,还有一个可选的参数,用于表示超时的。返回值是另一个总的请求者,它能够马上执行所有的请求者,但最终结果取得是最先成功的那次结果。如果数组中所有的请求者失败了,那么表示这个竞争失败。数组并没有改变。

如果传入毫秒数的参数,则表示是超时的参数。

If a milliseconds argument is provided, then the race will fail if itdoes not finish before the time limit.

RQ.fallback(requestors)RQ.fallback(requestors, milliseconds)

RQ.fallback 有一个请求者函数所构成的数组的参数,还有一个可选的参数,用于表示超时的。返回值是另一个总的请求者,虽然它也会如串行般依次执行,但只要有一个请求者执行成功了,那么剩余的请求者将不会执行。如果数组中所有的请求者失败了,那么表示这个串行队列失败。数组并没有改变。

那段雨骤风狂。人生之旅本就是风雨兼程,是要说曾经拥有,

Douglas Crockford 大神写的 JavaScript 异步控制库:RQ(下)

相关文章:

你感兴趣的文章:

标签云: