fcf.Actions class
Object for performing asynchronous actions. Implements the tasks of the Promise object, but does not break the execution task, saves the request context on the server side, and uses an extended set of methods.
Class description
Methods
Methods
fcf.Actions asyncEach(object|array|function a_collection, function a_cb)
Performs multiple parallel runs of the a_cb asynchronous task with the parameters specified in the a_collection parameter
Arguments:
object|array|function a_collection - A collection with parameters for starting an asynchronous task.
The parameter can be a function that should return a collection. The function call is made before the task is executed.
function a_cb - Callback of the asynchronous operation handler function, which is performed on each element of the a_collection collection
The function has two signatures on which the behavior of the method depends.
-
mixed func(mixed a_key, mixed a_value, mixed a_res) - The function action is considered completed after executing the method or after executing the returned one
the Promise|fcf.Actions object.
-
mixed func(mixed a_key, mixed a_value, mixed a_res, fcf.Actions.Act a_act) - The handler action is considered completed after calling fcf.Action.Act.сomplate() on the a_act or fcf.Action.Act.error() object if an error occurs.
mixed a_key - key of the collection item
mixed a_value - value of the collection element
mixed a_res - result of the previous handler
fcf.Actions.Act a_act - handler completion confirmation object
Return value:
fcf.Actions
Returns a pointer to itself
Examples
Example 1
Example of using the method
let actions = new fcf.Actions()
actions.asyncEach(["one", "two", "three"], (a_key, a_value, a_res, a_act)=>{
setTimeout(()=>{
console.warn(a_value);
a_act.complete();
}, Math.floor(Math.random() * 1000))
})
.then(()=>{
console.warn("complete");
})
Result:
three
one
two
complete
fcf.Actions catch(function a_cb)
Adds the error handler function to the set of deferred operations
Arguments:
function a_cb - Error handling function. Function signature: function(ErrorObject a_error);
Return value:
fcf.Actions
Returns a pointer to itself
fcf.Actions each(object|array|function a_collection, function a_cb)
Performs multiple runs of the a_cb asynchronous task with the parameters specified in the a_collection parameter
Arguments:
object|array|function a_collection - A collection with parameters for starting an asynchronous task.
The parameter can be a function that should return a collection. The function call is made before the task is executed.
function a_cb - Callback of the asynchronous operation handler function, which is performed on each element of the a_collection collection
The function has two signatures on which the behavior of the method depends.
-
mixed func(mixed a_key, mixed a_value, mixed a_res) - The function action is considered completed after executing the method or after executing the returned one
the Promise|fcf.Actions object.
-
mixed func(mixed a_key, mixed a_value, mixed a_res, fcf.Actions.Act a_act) - The handler action is considered completed after calling fcf.Action.Act.сomplate() on the a_act or fcf.Action.Act.error() object if an error occurs.
mixed a_key - key of the collection item
mixed a_value - value of the collection element
mixed a_res - result of the previous handler
fcf.Actions.Act a_act - handler completion confirmation object
Return value:
fcf.Actions
Returns a pointer to itself
Examples
Example 1
Example of using the method
let actions = new fcf.Actions()
actions.each(["one", "two", "three"], (a_key, a_value, a_res, a_act)=>{
setTimeout(()=>{
console.warn(a_value);
a_act.complete();
}, Math.floor(Math.random() * 1000))
})
Result:
one
two
three
fcf.Actions error(ErrorObject a_error)
Terminates the execution queue with an error
Arguments:
ErrorObject a_error - Error object
Return value:
fcf.Actions
Pointer to yourself
fcf.Actions finally(function a_cb)
The function adds a deferred action to the processing queue, which is executed both if an error occurs and if it succeeds
Arguments:
function a_cb - Callback of the handler function. Function signature: function()
Return value:
fcf.Actions
Pointer to yourself
fcf.Actions then(function a_cb, function a_cberror = undefined)
Puts the pending task in the execution queue passed in the a_cb parameter
Arguments:
function a_cb - Callback of the handler function.
The function has two signatures on which the behavior of the method depends.
-
mixed func(mixed a_res) - The function action is considered completed after executing the method, or if the Promise|fcf.Actions object was returned after executing the deferred action.
-
mixed func(mixed a_res, fcf.Actions.Act a_act) - The handler action is considered completed after calling fcf.Action.Act.сomplate() on the a_act object or fcf.Action.Act.error() if an error occurs.
mixed a_res - result of the previous handler
fcf.Actions.Act a_act - handler completion confirmation object
function a_cberror - Error handling function. Function signature: function(ErrorObject a_error);
Return value:
fcf.Actions
Returns a pointer to itself
Examples
Example 1
Example of using the fcf.Actions.then() method in the mode without confirming the completion of the operation.
async function test(){
let actions = new fcf.Actions();
return actions
.then(()=>{
return new Promise(function(resolve, reject) {
setTimeout(()=>{
console.warn("action 1");
resolve("001");
}, 1000);
});
}).
then((a_res)=>{
console.warn(`result action 1: ${a_res}`);
console.warn("action 2");
return "002";
})
}
let resultAction2 = await test();
console.warn(`result action 2: ${resultAction2}`);
console.warn("exit");
Result:
action 1
result action 1: 001
action 2
result action 2: 002
exit
Example 2
Example of using the fcf.Actions.then() method in the operation completion confirmation mode.
async function test(){
let actions = new fcf.Actions();
return actions
.then((a_res, a_act)=>{
setTimeout(()=>{
console.warn("action 1");
a_act.complete("001")
}, 1000);
}).
then((a_res)=>{
console.warn(`result action 1: ${a_res}`);
console.warn("action 2");
return "002";
});
}
let resultAction2 = await test();
console.warn(`result action 2: ${resultAction2}`);
console.warn("exit");
Result:
action 1
result action 1: 001
action 2
result action 2: 002
exit