簡體   English   中英

Async Await不使用async Await

[英]Async Await without using async Await

我試圖將我的代碼從使用Async / Await方法轉換為基本.then()promises。 原因是Async / Await不適用於IE。 我是承諾的新手,並且最近得到了使用Async / Await的掛起,但現在需要稍微轉換一下以使我的代碼在IE中工作。

使用異步/等待工作的代碼在這里上Codepen.io

請,任何幫助將不勝感激。

Javascript嘗試不使用Async / Await:

const getPromise = () => {  
return new Promise((resolve, reject) => {
    setTimeout(() => {
      $.getJSON( countriesUrl, function( data ) {
      }).done(function(data){
        resolve(data);
      }).fail(function(error){
        var reason = new Error('mom is not happy today');
        reject(reason);
     });
    }, 500);
  });
};


var bcp = {
init: function(){
  bcp.topbar = parseInt($('.topbar').css('height'), 10);
  bcp.bottombar = parseInt($('.bottom-bar').css('height'), 10);
  if(!bcp.countriesLoaded){
    console.log('testing');
      bcp.addCountries().then((countries) => {
        console.log('hello');
      bcp.popup = $($.fancybox.getInstance().current.src)[0];
      bcp.distributorArrays(countries);
    });
  }else {
    bcp.addEventListeners();
  }
},
toggleCountrySection: function(){
  $('#locationModal').find('.loading').toggleClass('show');
  $('.content').toggle();
},
getCountries: function() {
  console.log('get Countries');
  bcp.toggleCountrySection();
},
addCountries: function() {
  (() => {
      getPromise()
      .then(result => {
        console.log('result', result);
        var data = result;
        return data;
      }).then(function(data){
        var countries = data;
          bcp.toggleCountrySection();
          bcp.countriesLoaded = true;
          console.log('test', countries);
          return countries;
      })
      .catch(err => {
          console.log(err);
      });
  })();
};

我從未收到console.log('你好')。 所以我的函數bcp.addCountries()。then((countries)=> {})不會重新執行anytihng或者我覺得我沒有正確使用.then()。

這是我使用Async / Await的工作代碼:

init: function(){
  bcp.topbar = parseInt($('.topbar').css('height'), 10);
  bcp.bottombar = parseInt($('.bottom-bar').css('height'), 10);
  if(!bcp.countriesLoaded){
      bcp.addCountries().then((countries) => {
      bcp.popup = $($.fancybox.getInstance().current.src)[0];
      bcp.distributorArrays(countries);
    });
  }else {
    bcp.addEventListeners();
  }
},
toggleCountrySection: function(){
  $('#locationModal').find('.loading').toggleClass('show');
  $('.content').toggle();
},
getCountries: function() {
  console.log('get Countries');
  bcp.toggleCountrySection();
  return new Promise(resolve => {
    setTimeout(() => {
      $.ajax({
        url: countriesUrl,
        success: function(data) {
          var results = JSON.parse(data);
          resolve(results);
        }
      });
    }, 1500);
  });
},
addCountries: async function() {
  var countries = await bcp.getCountries();
  bcp.toggleCountrySection();
  bcp.countriesLoaded = true;
  return countries;
},

獲取工作版本( 來自您的評論Here is my working code using Async/Await: addCountries Here is my working code using Async/Await: addCountries並將addCountries更改為此。

使用的答案:

return bcp.getCountries().then((countries) => {
    console.log('test', countries);
    bcp.toggleCountrySection();
    bcp.countriesLoaded = true;
    return countries;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM