Javascript Angular async promise未正确链接以返回第一次呼叫以进行第二次呼叫
在我呼叫第二个服务之前,我想返回一个zipcode,这样——据我所知——我可以写一个承诺,然后稍后再要求承诺。所以我想我应该把我的第二次服务放在第一次服务的承诺里面。但是,以这种方式链接承诺是不友好的 调用角度工厂并在工厂方法内部:Javascript Angular async promise未正确链接以返回第一次呼叫以进行第二次呼叫,javascript,angularjs,asynchronous,promise,angular-promise,Javascript,Angularjs,Asynchronous,Promise,Angular Promise,在我呼叫第二个服务之前,我想返回一个zipcode,这样——据我所知——我可以写一个承诺,然后稍后再要求承诺。所以我想我应该把我的第二次服务放在第一次服务的承诺里面。但是,以这种方式链接承诺是不友好的 调用角度工厂并在工厂方法内部: var userGeoPromise = userService.getGeoposition().then(function (geoposition) { vm.geoposition = geoposition; return
var userGeoPromise = userService.getGeoposition().then(function (geoposition) {
vm.geoposition = geoposition;
return addressService.reverseGeocode(geoposition.coords);
}).then(function (data) {
vm.currentLocation = googleService.googleAddressComponentsToAddress(data.results[0]);
zipCodeCurrent = vm.currentLocation.zip;
});
userGeoPromise.then( function() {
console.log('should always show', zipCodeCurrent);
});
userGeoPromise.then( function() {
var serviceBase = "http://localhost:2295/api/getservicezip/"+ zipCodeCurrent;
var serviceZipPromise = $http.get(serviceBase);
return serviceZipPromise.then(function (results) {
console.log('serviceZipPromise', results);
return results.data;
});
});
注意上面的两件事:
var userGeoPromise
zipCodeCurrent
已设置,其中包含zipcodevar userGeoPromise = userService.getGeoposition().then(function (geoposition) {
vm.geoposition = geoposition;
return addressService.reverseGeocode(geoposition.coords);
}).then(function (data) {
vm.currentLocation = googleService.googleAddressComponentsToAddress(data.results[0]);
zipCodeCurrent = vm.currentLocation.zip;
});
userGeoPromise.then( function() {
console.log('should always show', zipCodeCurrent);
});
userGeoPromise.then( function() {
var serviceBase = "http://localhost:2295/api/getservicezip/"+ zipCodeCurrent;
var serviceZipPromise = $http.get(serviceBase);
return serviceZipPromise.then(function (results) {
console.log('serviceZipPromise', results);
return results.data;
});
});
第二次服务电话:
var userGeoPromise = userService.getGeoposition().then(function (geoposition) {
vm.geoposition = geoposition;
return addressService.reverseGeocode(geoposition.coords);
}).then(function (data) {
vm.currentLocation = googleService.googleAddressComponentsToAddress(data.results[0]);
zipCodeCurrent = vm.currentLocation.zip;
});
userGeoPromise.then( function() {
console.log('should always show', zipCodeCurrent);
});
userGeoPromise.then( function() {
var serviceBase = "http://localhost:2295/api/getservicezip/"+ zipCodeCurrent;
var serviceZipPromise = $http.get(serviceBase);
return serviceZipPromise.then(function (results) {
console.log('serviceZipPromise', results);
return results.data;
});
});
但是现在当我放置
服务zippromise时,站点模式只是旋转。。。在另一个承诺中。切换顺序并添加两个单独的错误处理程序(顺便说一句,这是一个很好的实践)
这应该不会出错,但对于真正的serviceBase
,要使用邮政编码,您可能需要稍后执行它
为您更新答案
// created part of the api call
var xserviceBase = "http://localhost:2295"; // this looks to be your base
return userGeoPromise.then(function(){
return $http.get(xserviceBase + '/api/getserviceablezip/' + zipCodeCurrent).then(function(results){
return results.data;
})
});
是的,我知道3个返回看起来有点糟糕,但是它应该在回调中工作,然后您应该返回结果值,而不是将其分配给zipCodeCurrent
(或任何其他变量)。您在第一次回调中正确地执行了此操作,然后在第二次回调中应用了相同的原则:
var userGeoPromise = userService.getGeoposition().then(function (geoposition) {
vm.geoposition = geoposition;
return addressService.reverseGeocode(geoposition.coords);
}).then(function (data) {
vm.currentLocation = googleService.googleAddressComponentsToAddress(data.results[0]);
return vm.currentLocation.zip; // *** return it
});
注意:我没有涉及到对vm
属性的赋值,但是通常应该避免变异(显然)存在于这些回调函数范围之外的变量
承诺测试如下所示:
userGeoPromise.then( function(zipCodeCurrent) { // *** add the argument
console.log('should always show', zipCodeCurrent);
});
第二个服务有一个嵌套的then
调用,这是需要避免的。不要对嵌套的中间承诺调用then
,而是返回该承诺,并在主承诺链上应用then
:
userGeoPromise.then( function(zipCodeCurrent) { // *** add the argument as in the test
var serviceBase = "http://localhost:2295/api/getservicezip/"+ zipCodeCurrent;
return $http.get(serviceBase); // *** return the promise
}).then( function (results) { // *** move the then-callback to the outer chain
console.log('serviceZipPromise', results);
return results.data;
}).catch( function (error) { // *** add error handling at the end of the chain
console.log('error occurred:', error);
});
请注意嵌套级别是如何从不超过1的。我认为它是googleService。googleAddressComponentsToAddress
不是异步的控制台消息中的任何内容?可能是$http.get(serviceBase)
正在拒绝(您绝对没有拒绝处理程序,因此在最后一段代码中有东西拒绝是理所当然的)好吧,我不确定谷歌服务的事情。。。但似乎只要我在呼叫其他服务之前等待,那就没关系了。早些时候,我甚至尝试调用一个函数,并将第二个服务包装在一个函数中,认为这样做会很好,因为它最终会被调用,但这也不起作用。我可以将第二个服务调用从承诺调用中拉出来,它起作用了,除了我必须硬编码zipcode返回serviceZipPromise。然后(…
我在互联网上找不到任何关于GoogleAddressComponentToAddress
-这是你自己的功能吗?总体来说,我喜欢它,没有错误,但问题仍然是serviceBase需要在里面,因为它需要传递邮政编码。我想,好的,我会给出一个更新的答案ting迟到了,所以我不打算进行所有的错误处理,但我的更新答案应该“有效”,以便zip通过。