Question:
When I access(for lack of a better term) the ‘/postUserInput’ POST route, it either does not run getGeoNamesData() until the end, or it doesn’t wait for the API fetch to finish. The data I get from this function is used for simultaneous API calls.What should I do to make sure these functions run in order?
This is my server file code. It runs in this order according to the console:
first third fourth fifth sixth seventh eigth second
This keeps all my remaining API calls from having the correct data. Please help.
Thanks.
Answer:
Please check the MDN Docs for async functions in Javascript. According to the docs:Async functions can contain zero or more await expressions. Await expressions make promise-returning functions behave as though they’re synchronous by suspending execution until the returned promise is fulfilled or rejected.
This should run the functions – getGeoNamesData, getCurrentWeatherbitData getPixabayData in order that you expect.
If you have better answer, please add a comment about this, thank you!