最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node.js + for loop + query execution - Stack Overflow

programmeradmin2浏览0评论

I am executing a for loop , inside that for loop i am executing an postgresql query and populating the result into an array. But i cant predict the execution flow.

my code:

var array =[];
       for(var i = 0 ; i< latitude.length; i++){       
    client.query("SELECT value->>'xxxx' as xxxx_details FROM yyyyy WHERE ST_DWithin(ST_GeogFromText('SRID=4326;POINT ("+latitude[i]+" "+longitude[i]+")'), geography(the_geom), " + radius + ")", function(err, row, fields) {               
                       array.push(row.rows[0]);
       }   
                console.log("bbbbbbbbb=" +array);

What i needed is i want the array to be printed after all the query executed inside the for loop. But now its printing before the array is populated. Help me to solve this. Thanks in advance..

I am executing a for loop , inside that for loop i am executing an postgresql query and populating the result into an array. But i cant predict the execution flow.

my code:

var array =[];
       for(var i = 0 ; i< latitude.length; i++){       
    client.query("SELECT value->>'xxxx' as xxxx_details FROM yyyyy WHERE ST_DWithin(ST_GeogFromText('SRID=4326;POINT ("+latitude[i]+" "+longitude[i]+")'), geography(the_geom), " + radius + ")", function(err, row, fields) {               
                       array.push(row.rows[0]);
       }   
                console.log("bbbbbbbbb=" +array);

What i needed is i want the array to be printed after all the query executed inside the for loop. But now its printing before the array is populated. Help me to solve this. Thanks in advance..

Share Improve this question asked Apr 21, 2014 at 11:15 SubburajSubburaj 5,20211 gold badges47 silver badges95 bronze badges 2
  • You're missing at least one closing })! – Bergi Commented Apr 21, 2014 at 11:35
  • Seems simple enough, keep a counter that you increment in the callback for the SQL, and when that counter reaches latitude length, you're all done. – adeneo Commented Apr 21, 2014 at 11:50
Add a ment  | 

1 Answer 1

Reset to default 6

The reason is client.query is async with results only available in the callback.

One option is async.js. Good write when to use what here.

From that article, you can execute code for each item in a collection. So, for your example, you can have an array of indexes or foreach to build up an array of sql query statements and then execute some for each query.

If queries was an array of queries, then something like:

async.forEach(queries, function(query, callback) {
    client.query(query, function(err, row, fields){
        array.push(row.rows[0]);
        callback(); // this signals async that you're done with this item
    });
}, function(err) {
    if (err) return next(err);

    // all queries are done here
});

Note there's also forEachLimit for doing n in parallel and forEachSeries which has limit of 1 (sequential).

EDIT:

A better option is async / await which is now available if you use typescript and pile to ES6+ and use node 4+ ( has generators ).

I cover the details in this sane node repo

A snippet from that repo shows awaiting an async call in a loop. It keeps it async and doesn't progress to the next line until it pletes. This also has the benefit of try / catch handling as you would expect it.

// await allows us to write linear code.  
// makes it easy to also call async code in a loop
// offers easy error handling with try catch
var quotes: IQuote[];
var tries: number = 0;
while (true) {
    try {
        ++tries;
        // ASYNC/AWAIT
        quotes = await this._store.find<IQuote>({});
        break;
    }
    catch (err) {
        if (tries == 3) { throw err; }
    }
}

return quotes;
发布评论

评论列表(0)

  1. 暂无评论