我有一个函数调用placeDecode,它接受一个HTML输入元素.在函数中,我有一个将输入值转换为格式化地址的promise.
我打了placeDecode两次电话,检查两个调用时是否已经解析我使用Promise.all,检查每个函数调用.当我尝试调用函数时出现问题viewResult,我得到Cannot read property 'viewresult' of undefined错误.
//call function twice
var startAddress = this.placeDecode1(this.searches.startSearch);
var endAddress = this.placeDecode1(this.searches.endSearch);
//promise all
Promise.all([endAddress,startAddress]).then(function(values) {
this.viewResult(values);
}).catch(function(result){
console.log(result);
})
console.log('hit');
}
//method convertes input into formatted address
private placeDecode1(input: HTMLInputElement) {
var result = new Promise(function(resolve, reject) {
var location = input.value;
var geoCode = new google.maps.Geocoder();
geoCode.geocode({
address: location
}, function(result,status){
if(status == 'OK'){
console.log(result[0].formatted_address);
resolve(result[0].formatted_address);
}
})
});
return result;
} …Run Code Online (Sandbox Code Playgroud)