0

I am trying to use multiple functions within a for loop but, one is returning undefined (or not waiting?)

Code is as below:

var arrNew = [];
var id = [];
var get1 = [];
var get2 = [];
var get3 = [];
var res = [];
var get4 = [];
for (j = 0; j < arrNew.length; j++) {
    id[j] = arrNew[j];
    get1[j] = await get1Fun(context, id[j]);
    if (get1[j] !== false) {
        get2[j] = await get2Fun(context, get1[j]);
        context.log(get2[j]);
        if (get2[j].length) {
            get3[j] = await get3Fun(context, get2[j]);
            if (get3[j] !== false && get3[j].status == 200) {
                res[j] = get3[j].result;
                get4[j] = await get4Fun(context, id[j], res[j]);
                context.log(get4[j])
            }
        }
    }
}
async function get1Fun(context, id) {
    const querySpec = {
        query: `SELECT * FROM r where r.id = "${id}"`,
    };
    const { resources } = await client.database(databaseId).container(containerId).items.query(querySpec).fetchAll();
        if (resources.length == 1) {
            return resources[0];
        } else if (!resources.length) {
            return false;
        }
}

function get2Fun(context, getInt) {
    return new Promise((resolve, reject) => {
        var msg = []
        // do for loop stuff with getInt and push stuff into msg
         
        context.log(msg)
        resolve(msg);
    });
}

The code works up until this point where get2Fun either returns undefined or if (get2[j].length) does not wait for await get2Fun.

Can someone clarify what is going wrong please?

I have tried with async function and without the promise.

The reason I have the arrays as below is because if I structured the function as var get1 = await get1Fun(context, id) then the for loop would stop after 1 or 2 items and not do every item in arrNew.

Thanks in advance.

EDIT:

Also tried the below but the for loop stops after 1 loop (presumably an issue with each loop redefining a variable?):

for (j = 0; j < arrNew.length; j++) {
    var id = arrNew[j];
    var get1 = await get1Fun(context, id);
    if (get1 !== false) {
        var get2 = await get2Fun(context, get1);
        context.log(get2);
        if (get2.length) {
            var get3 = await get3Fun(context, get2);
            if (get3 !== false && get3.status == 200) {
                var res = get3.result;
                var get4 = await get4Fun(context, id, res);
                context.log(get4)
            }
        }
    }
}
async function get1Fun(context, id) {
    const querySpec = {
        query: `SELECT * FROM r where r.id = "${id}"`,
    };
    const { resources } = await client.database(databaseId).container(containerId).items.query(querySpec).fetchAll();
        if (resources.length == 1) {
            return resources[0];
        } else if (!resources.length) {
            return false;
        }
}



   async function get2Fun(context, getInt) {

    
        var msg = []
        // do for loop stuff with getInt and push stuff into msg
         
        context.log(msg)
        return msg;
   
}
JDT
  • 965
  • 2
  • 8
  • 20
  • Shouldn't your operation be actually something like `get1Fun(context, id[j]).then(res1 => get2Fun(context, res1)).then(res2 => get3Fun(context, res2)).then(res3 => get4Fun(context, id[j], res3.result))`? I guess you can just make the handler not continue if the result is `false` (generically solvable as `maybeContinue = f => res => res !== false ? f(res) : res`). Then the entire thing can be thrown into `Promise.all`. I'm not really sure what's with all the tracking using multiple arrays. – VLAZ Nov 12 '20 at 18:46
  • @vlaz if I didn't track using multiple arrays then the for loop would stop after one item due to the awaits for some other reason ‍♂️ – JDT Nov 12 '20 at 18:54
  • But you don't really have to track everything. That's what the promises are for - handle each result so you don't have to write it down in an array then look it up later. It doesn't seem like there is really some relation between the results for different loops. They appear to be independent and so you don't even need to go in sequence, you can do all of this in parallel. – VLAZ Nov 12 '20 at 19:06
  • I tried that originally and the loop stopped after 1. I will post that code now - as above in the edits – JDT Nov 12 '20 at 19:34
  • (an issue related to this perhaps? https://stackoverflow.com/questions/44410119/in-javascript-does-using-await-inside-a-loop-block-the-loop) – JDT Nov 12 '20 at 19:41
  • Assuming all your operations are correct and the data you expect is not oddly shaped, I'd do [something like this](https://jsbin.com/yopuhacipo/1/edit?js,console) to handle these results relegating all the tracking to be done by the promises themselves. – VLAZ Nov 12 '20 at 19:45

0 Answers0