node.js - sqlite3 read all records in table and return

前端 未结 6 631
独厮守ぢ
独厮守ぢ 2020-12-19 06:38

I\'m trying to read all records in a sqlite3 table and return them via callback. But it seems that despite using serialize these calls are still ASYNC. Here is my code:

6条回答
  •  一个人的身影
    2020-12-19 07:17

    I tackled this differently, since these calls are asynchronous you need to wait until they complete to return their data. I did it with a setInterval(), kind of like throwing pizza dough up into the air and waiting for it to come back down.

    var reply = '';
    
    db.all(query, [], function(err, rows){
        if(err != null) {
            reply = err;
        } else {
            reply = rows;
        }
    });
    
    var callbacker = setInterval(function(){
        // check that our reply has been modified yet
        if( reply !== '' ){
            // clear the interval
            clearInterval(callbacker);
    
            // do work
    
        }
    }, 10); // every ten milliseconds
    

提交回复
热议问题