3

I am trying to convert my code from using Async/Await methods to basic .then() promises. The reason is that Async/Await does not work on IE. I am new to promises and came in late with finally getting a hang of using Async/Await but now need to convert back in time a little to make my code work in IE.

Working code using Async/Await here on Codepen.io

Please, any help would be greatly appreciated.

Javascript trying not to use 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); }); })(); }; 

I never receive the console.log('hello'). So my function bcp.addCountries().then((countries) => {}) is not retruning anytihng or i feel like i am not using the .then() properly.

Here is my working code using 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; }, 
5
  • 1
    May be using a transpiler like Babel will be easier for you. Commented Aug 21, 2018 at 17:13
  • medium.com/@zwacky/… Commented Aug 21, 2018 at 17:16
  • I do not want to use a build process or transpiler. Commented Aug 21, 2018 at 17:52
  • But ES6 promises do not work in Internet Explorer either? Commented Aug 21, 2018 at 18:03
  • I am using bluebird.js Commented Aug 21, 2018 at 18:16

1 Answer 1

2

Take the working version (from your comment Here is my working code using Async/Await:) and change addCountries to this.

Answer used:

return bcp.getCountries().then((countries) => { console.log('test', countries); bcp.toggleCountrySection(); bcp.countriesLoaded = true; return countries; }); 
Sign up to request clarification or add additional context in comments.

2 Comments

When I plugged your code in the bcp.addCountries is not returning countries and so when i console.log(countries) it comes up as undefined. I posted the codepen of the working code where you would be able to test it out if that helps.
Hey @Igor, I ended up using your code but return countries from within the function and it worked. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.