同步解析链接 Angular Promises
Resolving chained Angular Promises synchronously
我有一个使用 $q 服务的 resolve promise 函数,其中有一些通用代码 resolve/reject 基于特定条件。我有一个场景,其中只有在成功解析 api1 之后我才必须执行 api2。但是这两个调用都是异步发生的。我在下面粘贴了伪代码。请帮忙。非常感谢。
var resolvePromise = function(promise)
{
var defer = $q.defer();
promise.then(function(response)
if(certain conditions are true)
{
defer.reject(err)
}
defer.resolve();
)
.catch(function(errors){
defer.reject(errors);
})
return defer.promise;
}
function synchronousCalls()
{
var promise1 = service.getApi1();
var promise2 = service.getApi2();
return resolvePromise(promise1).then(function(){
return resolvePromise(promise2);
})
}
function getData()
{
synchronousCalls().then(function(){
console.log("synchronous run of apis ended");
})
}
将 synchronousCalls
更改为
function synchronousCalls()
{
return service.getApi1().then(resolvePromise).then(service.getApi2).then(resolvePromise);
}
您不需要 resolvePromise
函数。直接有getApi1
和getApi2
returnPromise
就可以.then()
。此外,调用 return Promise
的函数不会停止执行上下文。您将立即触发对两个 API 的调用,而不是等待第一个完成。您需要调用 getApi1()
,并且 .then()
应该从中 return 编辑 Promise
。考虑以下代码:
// This says, fire them both immediately
var promise1 = service.getApi1();
var promise2 = service.getApi2();
// Your call should look something like this
service
.getApi1()
.then(function (api1Response) {
// If I'm in here, I know that the request to service 1 is done
return service
.getApi2();
}).then(function (api2Response) {
// If I'm in here, I know that the request to service 2 is done
console.log(api2Response);
})
.catch(function (err) {
console.log("Something has gone wrong");
});
你们两个 getApi
函数应该看起来 一些东西 像这样,他们做的主要事情是 return 一些东西(比如 Promise
) 使用 .then()
方法。
function getApi1() {
// This is returning a Promise
return $http
.get("some/resource/on/the/server.json")
.then(function (response) {
/*
* Here I'm just peeling out the data from the response
* because I don't actually care about the details of the
* response, just the data
*/
return response.data;
});
}
我有一个使用 $q 服务的 resolve promise 函数,其中有一些通用代码 resolve/reject 基于特定条件。我有一个场景,其中只有在成功解析 api1 之后我才必须执行 api2。但是这两个调用都是异步发生的。我在下面粘贴了伪代码。请帮忙。非常感谢。
var resolvePromise = function(promise)
{
var defer = $q.defer();
promise.then(function(response)
if(certain conditions are true)
{
defer.reject(err)
}
defer.resolve();
)
.catch(function(errors){
defer.reject(errors);
})
return defer.promise;
}
function synchronousCalls()
{
var promise1 = service.getApi1();
var promise2 = service.getApi2();
return resolvePromise(promise1).then(function(){
return resolvePromise(promise2);
})
}
function getData()
{
synchronousCalls().then(function(){
console.log("synchronous run of apis ended");
})
}
将 synchronousCalls
更改为
function synchronousCalls()
{
return service.getApi1().then(resolvePromise).then(service.getApi2).then(resolvePromise);
}
您不需要 resolvePromise
函数。直接有getApi1
和getApi2
returnPromise
就可以.then()
。此外,调用 return Promise
的函数不会停止执行上下文。您将立即触发对两个 API 的调用,而不是等待第一个完成。您需要调用 getApi1()
,并且 .then()
应该从中 return 编辑 Promise
。考虑以下代码:
// This says, fire them both immediately
var promise1 = service.getApi1();
var promise2 = service.getApi2();
// Your call should look something like this
service
.getApi1()
.then(function (api1Response) {
// If I'm in here, I know that the request to service 1 is done
return service
.getApi2();
}).then(function (api2Response) {
// If I'm in here, I know that the request to service 2 is done
console.log(api2Response);
})
.catch(function (err) {
console.log("Something has gone wrong");
});
你们两个 getApi
函数应该看起来 一些东西 像这样,他们做的主要事情是 return 一些东西(比如 Promise
) 使用 .then()
方法。
function getApi1() {
// This is returning a Promise
return $http
.get("some/resource/on/the/server.json")
.then(function (response) {
/*
* Here I'm just peeling out the data from the response
* because I don't actually care about the details of the
* response, just the data
*/
return response.data;
});
}