我有三个cordova.exec调用并执行aysnc。我的目标是在完成所有三个电话后拨打第四个cordova电话。我无法嵌套调用以使其同步,但我需要在三次调用完成后调用第四个(例如,承诺/在jquery中)。
答案 0 :(得分:3)
您需要Cordova promise polyfil。然后像其他人一样编写代码:
//exec as promise
var execPromise = function(){
return new Promise(
function(resolve,reject){
cordova.exec.apply(
cordova,
[
resolve,
reject
].concat(
Array.prototype.slice.apply(arguments)
)
)
}
);
}
Promise.all(
[
execPromise(args),
execPromise(args),
execPromise(args)
]
)
.then(
function(results){
console.log("three are done, results are:",results);//array of results
return execPromise(args);
}
)
.then(
function(result){
console.log("all are done, result:",result);
}
)
.catch(
function(err){
console.log("something went wrong, error is:",err);
}
);