小编Yus*_*dar的帖子

Javascript,在promise中调用函数

代码说明

我有一个函数调用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)

javascript asynchronous promise typescript

0
推荐指数
1
解决办法
1132
查看次数

标签 统计

asynchronous ×1

javascript ×1

promise ×1

typescript ×1