Categories
JavaScript Answers

How to assign value from successful promise resolve to external variable with JavaScript?

Spread the love

Sometimes, we want to assign value from successful promise resolve to external variable with JavaScript.

In this article, we’ll look at how to assign value from successful promise resolve to external variable with JavaScript.

How to assign value from successful promise resolve to external variable with JavaScript?

To assign value from successful promise resolve to external variable with JavaScript, we use async and await.

For instance, we write

const myFunction = async () => {
  vm.feed = await getFeed();
  // ...
};

to get the resolve value of the promise returned by getFeed with await.

Then we assign the returned value to vm.feed.

We have to use await inside async functions.

Conclusion

To assign value from successful promise resolve to external variable with JavaScript, we use async and await.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *