Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 17, 2022 05:17 am GMT

Loop functions in synchronous way React Quick Notes.

We all know term called Recursion. That is same we are doing in below example to loop through multiple functions which should execute one after another.

This is one of the ways we can achive function serialization and can be usfull to your application.

const objFunctions = [  { func: function1, args: args1 },  { func: function2, args: args2 }];  //We defined functions that we want to call one by one.function LoopFunctionOneByOneCallAfterOneAnother(objFunctions) {  if (!objFunctions) process.exit(0); // finished  ExecuteSingleFunctionCall(objFunctions, function (result) {    // continue AFTER callback    LoopFunctionOneByOneCallAfterOneAnother(objFunctions.shift());  });}  // notice we called same function under this function which has parameter to get next object from objFunctions.function ExecuteSingleFunctionCall(objFunction, callback) {  const { args, func } = objFunction;  func(args, callback);} // Supportive function to execte single function. LoopFunctionOneByOneCallAfterOneAnother(objFunctions.shift()); // In the end we call our function.

So this whole block will execute in serialized manner.
Above referenced from site https://nodejs.dev/en/learn/asynchronous-flow-control/, you can find more usefull ways over here.


Original Link: https://dev.to/ajaybaraiya6/loop-functions-in-synchronous-way-react-quick-notes-gbh

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To