Skip to content
Advertisement

Proper way of using Async functions with loops inside in NodeJS

I can see this code to be over 9000 percent easier to be performed in Java but the client wants NodeJs hence the struggle:

Part1:

async functions in NodeJs and Js scare me. Mainly cause there are so many ways of resolving them: Promices, Callbacks, async await. I have went through multiple tutorials and explanation documents trying to figure out how any of them work but eventually in half of the cases I end up using eventEmitter as it is much clearer and straightforward to use. Does anyone have their favourite resourse to address to understand async functions properly?

So far I have this solution:

function a (){
.......

 (async()=>{
   await anotherAsyncFunction()
  })()

}


//somewhere further down the code
function anotherAsyncFuncion(){
  return anotherAsyncFunctionWithCode()
}

//and finally
async function anotherAsyncFunctionWithCode(){
//some action
}

This more of less functional piece of code I have found on the realms of StackOverflow and so far at least it does what it has to ..but why do I need to go through a regular sync function function anotherAsyncFuncion()in order to get the result? Also, why does the function with await has those brackets at the end of the async? (async()=>{ await anotherAsyncFunction() })()

Part2:

All the struggle above is smth I still cant get and because of that I do not manage to properly establish the following code:

Task: I am sending a post request to the server, passing a query with the parameters for filtering data. The query can have such a way:

var query ={
 time: "some time here",
 paging:{
    pageSize: 100,
    pageNumber:1
  }
}

when I send the request, the result has the following schema:

{
"entities":[well, a lot of values here],
"pageSize":100,
"pageNumber":1,
"pageCount":4
}

My code has to pick up all the data from every page and treat it (just take some values like name , surname etc).the server (sometimes) tells me what is the total value of data "pageCount":4. Sometimes it doesnt.

My attempts: So far , apart everything I have tried to send request 1 time, fetch pagenumber and then iterate with for (var i=0; i<pageNumber; i++){send post request with pageNumber equals i} I’ve also tried while (data!== undefined){send request, treat data, increase page, call same function from within itself} and everytime the program runc asynchronously, meaning…it’s a mess and i cant be sure all the data is there.

Can you propose a proper way to deal with this kind of requests ?

Hope it’s not too messy, if you have any suggestions about proper asking questions tips, shoot as well 🙂

Advertisement

Answer

function with await does not require those brackets at the end. You could write it without it. In your particular use case, it is absolutely not required.

Here is more info on immediately-invoked functions

You do not need to go through a regular function to get the result, If the module which you are using to make an API call can returns a promise you can use that directly.

For example, using promise

const request = require('request-promise');

request.get('https://example.com')
.then(function(body) {
    console.log('Body:', body);
});

much more simple syntax using async/await

const request = require('request-promise');

async function main() {
    var body = await request.get('https://example.com');
    console.log('Body:', body);
}
main()

Coming to your use case, if you want to make multiple API calls until the condition is met, you can make use of recursion. If you are not sure of the pageCount attribute in the response, then you could compare the entity attribute for validness.

Below is an example to recursively call an endpoint until the required condition is met.

const rp = require('request-promise')

let count = 0;
async function main() {
    console.log(`${count++}`);
    let res = await rp.get('https://jsonplaceholder.typicode.com/todos/1')
    json_res = JSON.parse(res);
    
    // process the response here
 
    // setting hard limit
    if(count == 10) process.exit();

    if(json_res.completed == false) {
        main()
    } else {
        //... continue with your code
    }
}

main()

You can also store the API response in a variable and return the same if the exit condition is met.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement