简体   繁体   English

闭包和回调

[英]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: 我正在研究数据库函数,需要从fetchAll返回结果,所以我可以在我的代码中的其他地方使用它,但我不知道该怎么做:

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 ? 找到getResults的同步版本,通常有一个函数,也许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!) 使用节点,您可以使用https://github.com/laverdet/node-fibershttp://github.com/maxtaco/tamejs从异步转换为同步样式(我目前正在使用tamejs,它是大!)
  • And others I don't know enough to talk about 和其他我不太了解的人谈论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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