我使用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');
}
有没有办法使这个功能同步:不执行line2直到line1完成(或查询失败)?
由于
答案 0 :(得分:2)
没有。一旦函数是异步的,就没有真正的同步方法,但你可以链接承诺。即
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