1

I would like to be able to implement some complex business logic in JavaScript using procedural approach, that exists in other languages, like Java or VB.

For example, following pseudocode could be easily implemented using procedural programming:

function doThings(){
    var data = getDataFromApi1();
    var res=[];
    for(el of data) {
        if(getSomethingFromAPI2(e3)) 
            res.push(el);
        else
            if(getSomethingElseFromAPI3(e3))
                res.push(getSomethingDifferentFromAPI4(el));
    }
    return saveToSomeApi5(res);
}

But due to the fact, that JavaScript is event driven, all this logic needs to be transformed somehow into reactions on events. So, from what I researched, my options are callbacks, promises and async/await. But each of these methods have disadvantages:

Callbacks - I get callback hell, and also I have to do weird things, like process arrays via recursion. It all makes complex hard-to-maintain code.

Promises - I have to split the code above into separate functions, because each next promise depends on result from previous one, so I cannot just connect them with .then()s. Code becomes split into many pieces that are not following the logic flow, therefore it also becomes hard to read.

Async/await - I have to manually mark and somehow trace all async/await functions and all functions that are calling them, which seem like a tedious work. Also, they all have to return promise, so I cannot just transform all functions in the project into async/await.

Are there any other options in JavaScript to implement this type of logic in more procedural/imperative way?

Daniel
  • 94
  • 4

0 Answers0