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

前端 未结 6 630
独厮守ぢ
独厮守ぢ 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:24

    A promise based method

    var readRecordsFromMediaTable = function(){
      return new Promise(function (resolve, reject) {
        var responseObj;
        db.all("SELECT * FROM MediaTable", null, function cb(err, rows) {
          if (err) {
            responseObj = {
              'error': err
            };
            reject(responseObj);
          } else {
            responseObj = {
              statement: this,
              rows: rows
            };
            resolve(responseObj);
          }
          db.close();
        });
      });
    }
    

提交回复
热议问题