0

I want to get the mean value for a timespan from influxdb for multiple nodes. I´m using the promise function to get synchronous results.

The Problem:
The promise is still pending, even after the function finishes.

How can I solve thist matter?

I`m relative new to NodeJs, so please no hate.

System Setup:
OS: Ubuntu 18.04 LTS (Server) | NodeJs: v12.8.1 | INfluxDB shell: 0.12.2

Here is the code I´m working with. I added console.logs to see where the errors are.

function parseData(go1, go2, mac, zeit, meter)     {
        var anz = go1[0].count;
        var transfer = [];
        for (i = 0; i < anz; i++)   {

            transfer.push(checkValue(i, go2, mac, zeit, meter, anz))

        }
        console.log(transfer);
}

function checkValue(i, go2, mac, zeit, meter, anz){
    return new Promise(function(resolve, reject){
        var Node = go2[i].distinct;
        var query = `select mean(rssi) from "` + mac +`" where time > (now() - ` + zeit + `m) AND rssi < ` + meter + ` AND Node = '` + Node + `'`;
        console.log(query);
        influx.query(
            query
        )
        .then(
            result=>{
                var pars = result;
                var res = pars[0].mean;
                var eintrag = [];
                eintrag.push(Node);
                eintrag.push(res);
                console.log(eintrag);
                return resolve([eintrag]);                              
            }
        );    
    });
}

And here are my current console.log

select mean(rssi) from "75*****500eb" where time > (now() - 3600m) AND rssi < 20 AND Node = 'ble*1*pi'
select mean(rssi) from "75*****500eb" where time > (now() - 3600m) AND rssi < 20 AND Node = 'ble*2*pi'
[ Promise { <pending> }, Promise { <pending> } ]
[ 'ble*1*pi', 9.027241718321147 ]
[ 'ble*2*pi', 18.86637795689184 ]
  • 2
    Possible duplicate of [How to return many Promises in a loop and wait for them all to do other stuff](https://stackoverflow.com/questions/31426740/how-to-return-many-promises-in-a-loop-and-wait-for-them-all-to-do-other-stuff) – CertainPerformance Sep 03 '19 at 07:54

1 Answers1

-1

When the console.log is executed, the promises haven't yet completed.

Please have a look at this question: How to return many Promises in a loop and wait for them all to do other stuff

I would suggest you to use await Promise.all:

const main = async () => {
   const arrayOfResults = await Promise.all([checkValue(...), checkValue(...)])
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

In your specific case you can do the following:

async function parseData(go1, go2, mac, zeit, meter)     {
        var anz = go1[0].count;
        var transfer = [];
        for (i = 0; i < anz; i++)   {

            transfer.push(checkValue(i, go2, mac, zeit, meter, anz))

        }
        console.log(await Promise.all(transfer));
}
Scaccoman
  • 455
  • 7
  • 16
  • When a question is a clear duplicate of another, please flag / vote to close the question as a duplicate, rather than posting another answer using the same information gotten from the canonical. If you have anything new to add, add it to the canonical Q+A. – CertainPerformance Sep 03 '19 at 09:45