同步微风ExecuteQuery

同步微风ExecuteQuery

本文介绍了同步微风ExecuteQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Breeze promise ExecuteQuery从这样的数据库中获取数据:

I get data form the DB like this using Breeze promise ExecuteQuery :

var getdata = function(){
    var manager = new breeze.EntityManager(serviceName);
    var query = new EntityQuery().from('MyTable');
    manager.executeQuery(query)
                .then(function(data){
                     //line1
                     console.log('success');
                });
    //line2
    console.log('end');
}

是否有任何方法可以使此函数同步:在完成第1行(或查询失败)之前不执行第2行?

Is there any way to make this function synchronous : not executing line2 untill line1 is done (or query failed) ?

谢谢

推荐答案

否.一旦函数异步,就没有真正的方法可以使同步,但是您可以链接诺言.即

No. Once a function is async there is no real way to make synchronous, but you can chain promises. i.e.

var getdata = function(){
    var manager = new breeze.EntityManager(serviceName);
    var query = new EntityQuery().from('MyTable');
    manager.executeQuery(query).then(doThis).then(doThat);
}

function doThis(data) {
  console.log('success');
}

function doThat() {
  console.log('end');
}

var getdata = function() {
    var manager = new breeze.EntityManager(serviceName);
    var query = new EntityQuery().from('MyTable');
    manager.executeQuery(query).then(function(data) {
      console.log('success');
    }).then(function() {
      console.log('end');
    }

或者您可能要考虑让getData函数本身返回一个Promise.您可以在此处了解更多信息: https://github.com/kriskowal/q

Or you might want to consider having your getData function itself return a promise. You can read more about this here: https://github.com/kriskowal/q

这篇关于同步微风ExecuteQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 21:19