Avatar of Bruce Gust
Bruce Gust
Flag 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?
Node.jsJavaScript

Avatar of undefined
Last Comment
Bruce Gust

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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!
Bruce Gust

ASKER
Smokin'!
Julian Hansen

You are welcome. (will respond in your other thread)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Bruce Gust

ASKER
Rock on!