Yus*_*dar 0 javascript asynchronous promise typescript
我有一个函数调用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)
我遇到的问题是,当我打电话时this.viewResult(values);,我得到了一个
Cannot read property 'viewResult' of undefined 错误.
非常感谢
更改:
function(values) {
this.viewResult(values);
}
Run Code Online (Sandbox Code Playgroud)
至:
(values) => {
this.viewResult(values);
}
Run Code Online (Sandbox Code Playgroud)
箭头函数this基于外部范围进行绑定.
| 归档时间: |
|
| 查看次数: |
1132 次 |
| 最近记录: |