javascript - 不使用异步等待的异步等待

标签 javascript promise async-await

我正在尝试将我的代码从使用 Async/Await 方法转换为基本的 .then() promise 。原因是 Async/Await 在 IE 上不起作用。 我是 promises 的新手,来晚了,终于掌握了使用 Async/Await 的窍门,但现在需要稍微及时转换一下,以使我的代码在 IE 中运行。

使用 Async/Await 的工作代码 here在 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('hello')。所以我的函数 bcp.addCountries().then((countries) => {}) 没有重新运行,或者我觉得我没有正确使用 .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;
},

最佳答案

获取工作版本(来自您的评论 这是我使用 Async/Await 的工作代码:)并将 addCountries 更改为此。

使用的答案:

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

关于javascript - 不使用异步等待的异步等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51953585/

相关文章:

php - 使用 jquery $.get 函数从服务器检索数据

javascript - jquery .next ('input' ).focus()

javascript - 如何用 Nightwatch 履行 promise

javascript - 使用 Promise 从映射函数内部推送到外部对象

c# - Stream.ReadAsync 和 Stream.WriteAsync 是否应该在返回之前或操作完成之后同步更改光标位置?

javascript - javascript中长时间跳跃的 Sprite ?

javascript - 如何使用 Javascript/Jquery/HTML5 函数将 HTML 页面捕获为图像

javascript - NodeJS 函数返回已解析的 Promise 值而不是 Promise?

c# - UWP 应用 : FileOpenPicker PickSingleFileAsync() can't await

c# - 在异步任务和 UI 线程中提高 PropertyChanged