简体   繁体   中英

Closures and callbacks

I'm working on a database function and need to return results from fetchAll so I can use it elsewhere in my code but am not sure how to do it:

function fetchAll(sql,params,tableref){
  var fields = new Array();
  var resultout = new Array();

  for (i in tableref){     
    fields.push(i);  
  }       

  getResults(sql,params,fields,function(results){
     // I WANT TO RETURN RESULTS
     resultout.push(results);       
  });          

// TO HERE SO I CAN RETURN from Fetchall
console.log(resultout);
}

function getResults(query,params,fields,callBack){
  var result = new Array(); 
  thisDB.transaction(function (tx) {
    tx.executeSql(query,params, function(tx, rs){
       for(var i=0; i<rs.rows.length; i++) {
          var row = rs.rows.item(i);
          var rowresults = new Object();

          for (x=0;x<fields.length;x++){
            rowresults[fields[x]] = row[fields[x]];      
          }

          result.push(rowresults);
       }
       callBack(result);
    });
  }); 

return result;  
}

I think i'm missing something obvious.

Thanks

Antony

If I'm understanding correctly your question, to see the result you need this

function fetchAll(sql,params,tableref){
    var fields = new Array();
    var resultout = new Array();

    for (i in tableref){     
        fields.push(i);  
    }       

    getResults(sql,params,fields,function(results){
        // I WANT TO RETURN RESULTS
        resultout.push(results);
        console.log(resultout);
    });
}

The callback will be executed "after" so basically in your example you'd see an empty result. This is because of the asynchronous nature of the callback.

I think you are trying to move from an asynchronous request to a synchronous one

Is a good topic, you can find many posts and solutions addressing this on the web

You have several options:

  • Use callback instead return in your function: Your function must receive another param (the callback) and call that callback passing to it the value that you want to return

     function fetchAll (sql, params, tableref, callback) { var fields = new Array(); for (i in tableref) { fields.push(i); } getResults(sql, params, fields, function (results) { // I WANT TO RETURN RESULTS callback(results); }); } 

    Then you can log the results like this:

     fetchAll(sql, params, tableref, function (results) { console.log(results); }); 
  • Find a synchronous version of getResults , often there is one function for that, maybe getResultsSync ?

  • With node you can use https://github.com/laverdet/node-fibers or http://github.com/maxtaco/tamejs for converting from asynchronous to synchronous style (I'm using tamejs for that purpose currently and it is great!)
  • And others I don't know enough to talk about

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM