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

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.