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

fcf.each() function

fcf.Actions->mixed fcf.each(mixed a_object, function a_cb)

Package: fcf-framework-core

File: fcf-framework-core:fcf.js

Available from version: 2.0.2

Iterates over the elements in the a_object argument. Iteration ends if the callback function returns a value other than undefined.

The function is used for unified enumeration of objects, enumerable objects (but not strings). Iterates over the elements of the a_object argument. Iterates until the last element, or until the a_cb functor returns a non-undefined result, Promise returns a non-undefined value, or an object fcf.Actions returning a value other than undefined

If the a_object argument is not an enumerable object, the function will exit without error

Arguments

mixed a_object
- Enumerated object

function a_cb
- The callback function to be called for each element. The function can be asynchronous and can also return fcf.Actions or Promise objects

Function signature: [simple or async] mixed a_cb(mixed a_key, mixed a_value)

Result
fcf.Actions->mixed
- Returns an fcf.Actions object containing the result of the last value returned by the a_cb function

Example: Function application

fcf.each({k1: "v1", k2: "v2", k3: "v3"}, (a_key, a_value)=>{ console.log("key:", a_key, "value:", a_value); });

Output:

key: k1 value: v1 key: k2 value: v2 key: k3 value: v3

Example: Async mode

console.log("Async function (with Promise):"); await fcf.each({k1: "v1", k2: "v2", k3: "v3"}, async (a_key, a_value)=>{ await new Promise((a_resolve, a_reject)=>{ setTimeout(()=>{ console.log(new Date(), "key:", a_key, "value:", a_value); a_resolve(); }, 1000); }); }); console.log(""); console.log("Result Promise:"); await fcf.each({k1: "v1", k2: "v2", k3: "v3"}, (a_key, a_value)=>{ return new Promise((a_resolve, a_reject)=>{ setTimeout(()=>{ console.log(new Date(), "key:", a_key, "value:", a_value); a_resolve(); }, 1000); }); }); console.log(""); console.log("Async function (with fcf.Actions):"); await fcf.each({k1: "v1", k2: "v2", k3: "v3"}, async (a_key, a_value)=>{ await fcf.actions() .then((a_result, a_act)=>{ setTimeout(()=>{ console.log(new Date(), "key:", a_key, "value:", a_value); a_act.complete(); }, 1000); }); }); console.log(""); console.log("Result fcf.Actions:"); await fcf.each({k1: "v1", k2: "v2", k3: "v3"}, (a_key, a_value)=>{ return fcf.actions() .then((a_result, a_act)=>{ setTimeout(()=>{ console.log(new Date(), "key:", a_key, "value:", a_value); a_act.complete(); }, 1000); }); });

Output:

Async function (with Promise): 2023-02-18T02:28:24.228Z key: k1 value: v1 2023-02-18T02:28:25.230Z key: k2 value: v2 2023-02-18T02:28:26.232Z key: k3 value: v3 Result Promise: 2023-02-18T02:28:27.234Z key: k1 value: v1 2023-02-18T02:28:28.234Z key: k2 value: v2 2023-02-18T02:28:29.236Z key: k3 value: v3 Async function (with fcf.Actions): 2023-02-18T02:28:30.238Z key: k1 value: v1 2023-02-18T02:28:31.240Z key: k2 value: v2 2023-02-18T02:28:32.241Z key: k3 value: v3 Result fcf.Actions: 2023-02-18T02:28:33.242Z key: k1 value: v1 2023-02-18T02:28:34.244Z key: k2 value: v2 2023-02-18T02:28:35.246Z key: k3 value: v3

Example: Interrupting execution and searching for an element

Note that to get the result of executing the fcf.each function in synchronous mode, you must execute the result method of the returned fcf.Actions object

{ console.log("Sync mode..."); let result = fcf.each({k1: "v1", k2: "v2", k3: "v3"}, (a_key, a_value)=>{ console.log("[Iteration] ", "key:", a_key, "value:", a_value); if (a_value == "v2"){ return a_key; } }).result(); console.log("Found key: ", result); } console.log(""); console.log("Async mode..."); let result = await fcf.each({k1: "v1", k2: "v2", k3: "v3"}, async (a_key, a_value)=>{ return await (new Promise((a_resolve, a_reject)=>{ setTimeout(()=>{ console.log("[Iteration] ", "key:", a_key, "value:", a_value); a_resolve(a_value == "v2" ? a_key : undefined); }, 10); })); }); console.log("Found key: ", result);

Output:

Sync mode... [Iteration] key: k1 value: v1 [Iteration] key: k2 value: v2 Found key: k2 Async mode... [Iteration] key: k1 value: v1 [Iteration] key: k2 value: v2 Found key: k2