A synchronous Breeze ExecuteQuery

2019-09-01 13:00发布

问题:

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');
}

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

Thanks

回答1:

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');
}

or

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');
    }

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