FCF 2.0 development in progress...
> > > > > >
[News] [Packages API] [Downloads] [Donate to the project] [Contacts]

asyncEach() method from fcf.Actions class

fcf.Actions asyncEach(object|array|function a_obj, function a_cb)

Package: fcf-framework-core

File: fcf-framework-core:fcf.js

Available from version: 2.0.2

Iterates over the passed object, calling a separate a_cb handler for each element of the object, the handlers for the passed set being executed simultaneously asynchronously. The iterable object can be passed to a function that returns the iterable object. In this case, the object acquisition will be performed immediately before the handler function is executed.

Arguments

object|array|function a_obj
- An enumerable object for each element of which the task handler function will be called, which will be placed in the general queue for processing asynchronous actions.

If the a_obj is given by a function, then before calling the handler function, the function passed in the a_obj argument is called and its result is used as an object for iteration.

function a_cb
- Asynchronous operation handler function. This function is called for each element of the a_obj object. The result returned by the function will be passed to the next handler. A function can have two signatures on which its behavior depends.
  • mixed a_cb(mixed a_key, mixed a_value, mixed a_lastResult) - function without completion confirmation. The function can be asynchronous. The execution of the next handler begins after the function has completed or the Promise or fcf.Actions object it returned has completed.

  • mixed a_cb(mixed a_key, mixed a_value, mixed a_lastResult, fcf.Actions.Act a_act) - function that confirms the completion of an asynchronous operation. The execution of the next handler starts after the call to the complete(mixed a_value) or error(Error a_error) method in case of an error on the a_act object.

Result
fcf.Actions
- Self pointer

Example: Function appilcation

let actions = new fcf.Actions(); await actions .then(()=>{ return "first result"; }) .asyncEach({k1: "v1", k2: "v2", k3: "v3"}, (a_key, a_value, a_lastResult, a_act)=>{ setTimeout(()=>{ console.log("Iteration: a_key:", a_key, "; a_value:", a_value, "; a_lastResult: ", a_lastResult); a_act.complete(a_value); }, Math.random()*100); }) .then((a_lastResult)=>{ console.log("Complete: a_lastResult:", a_lastResult); });

Output:

Iteration: a_key: k2 ; a_value: v2 ; a_lastResult: first result Iteration: a_key: k3 ; a_value: v3 ; a_lastResult: first result Iteration: a_key: k1 ; a_value: v1 ; a_lastResult: first result Complete: a_lastResult: v1