Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How would I convert this promise to an async / await function?

Here's my Promise:

$(document).ready(function() {
	
	const fetchData = () => {
	  const promise = new Promise((resolve, reject) => {
		setTimeout(() => {
		  resolve("Loose Cannon Fitness");
		}, 100);
	  });
	  return promise;
	};

	setTimeout(() => {
	  console.log("Smith and Gone");
	  fetchData()
	  .then(text => {
		console.log(text);
	  });
	}, 200);

});

Open in new window


How would I convert that to an "async / await" dynamic?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Julian!

As always, thank you for a thorough explanation!

What you've got works, but I've got one more related question and let me qualify it by saying that at this point, it's not about practical application as much as it's for my own edification.

Could I write "fetchData" as an anonymous function? Not that I would necessarily do that in a practical setting, but I was curious.

You'll find that question at https://www.experts-exchange.com/questions/29172470/Can-This-Code-Work-Using-an-Anonymous-Function.html

As always, thank you for your time, bud!
Smokin'!
You are welcome. (will respond in your other thread)
Rock on!