![](/img/trans.png)
[英]Why does my 2nd javascript promise call return the same value as the 1st?
[英]Angular async promise is not chaining correctly to give back 1st call in order to make 2nd call
我想在調用第二項服務之前返回一個郵政編碼,以便-根據我的了解-我可以包裝一個承諾,然后稍后再要求該承諾。 所以我想我將把第二項服務放在第一項服務的承諾之內。 但是鏈接承諾這種方式並不友好。
角工廠調用和內部工廠方法:
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;
});
注意上面的兩件事:
var userGeoPromise
zipCodeCurrent
,其中包含郵政編碼 承諾測試工作正常:
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;
});
});
但是現在當我將serviceZipPromise.then
... 放入另一個promise時,站點模式只是旋轉了。
切換順序並添加2個單獨的錯誤處理程序(順便說一句,這是一個好習慣)
var serviceZipPromise = $http.get(serviceBase); // call up
return serviceZipPromise.then(function (results) {
console.log('should always show', zipCodeCurrent);
userGeoPromise.then(function () {
//console.log('serviceZipPromise', results);
console.log('inside servicezip ', zipCodeCurrent);
}, function (err) { // error from userGeoPromise
console.log(err);
});
return results.data; // THIS will return the data
}, function (err) { // outer error, this was switched
console.log(err);
});
這應該不會出錯,但是為了使您的真實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個退貨看起來有點討厭,但是應該可以
在then
回調中,您應該返回結果值,而不是將其分配給zipCodeCurrent
(或任何其他變量)。 您在第一個then
回調中正確地做到了這一點,但是在第二個中應該應用相同的原理:
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
屬性的分配,但是通常您應該避免對那些(顯然)存在於這些回調函數范圍之外的變量進行突變。
Promise測試如下所示:
userGeoPromise.then( function(zipCodeCurrent) { // *** add the argument
console.log('should always show', zipCodeCurrent);
});
第二個服務有一個嵌套的then
調用,這是可以避免的。 而不是在嵌套的中間promise上調用then
,而是返回該promise,然后將then
應用於主promise鏈:
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.