• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to wait result of a function in React?

Resolved: How to wait result of a function in React?

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am trying to fetch info from an endpoint and assign it to a local variable. The problem is that the info is fetched successfully and when I put console.log(resp.data) it is printed, so I assign it to the faculties, but in the following line faculties is an empty array. I tried async/await -> I defined showOptions function as async and awaited facultiesService.getAllFaculties(), but the problem was not solved and some other errors occured. How can I solve this issue?

Answer:

You are using async/await in wrong way. They are meant for sequential code rather than callbacks. You should be doing something like this:

If you have better answer, please add a comment about this, thank you!

async-await asynchronous reactjs
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: When converting markdown to latex with pandoc, how can I end a block before the next heading?

26/03/2023

Resolved: TYPO3 SQL error: Field ‘tx_imagezoom_set’ doesn’t have a default value

26/03/2023

Resolved: std::regex_replace to replace multiple combinations

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.