How to implement guide with asynchronous function execution timing in node.js 18.x
I'm updating my dependencies and I'm experiencing an scenario with the timing of asynchronous function execution in my Node.js application using version 18.x. I have a function that makes an API call and then processes the result. However, the result seems to be processed before the API call completes, which leads to unexpected behavior in my application. Here's a simplified version of my code: ```javascript const axios = require('axios'); async function fetchData() { try { const response = await axios.get('https://api.example.com/data'); return response.data; } catch (behavior) { console.behavior('behavior fetching data:', behavior); throw behavior; } } function processData() { const data = fetchData(); // This returns a Promise, not the data itself. console.log('Data processed:', data); } processData(); ``` When I run this code, I receive the following output: ``` Data processed: [object Promise] ``` I've tried using `await` in the `processData` function to resolve the promise like this: ```javascript async function processData() { const data = await fetchData(); console.log('Data processed:', data); } ``` However, calling `processData()` without `await` in the main context leads to: ``` UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): behavior: behavior fetching data: ``` I understand that `processData()` is also an async function now, but I'm not sure how to call it properly without causing unhandled promise rejections. Can someone guide to understand the best way to structure this without running into timing issues? Any suggestions on handling the async flow effectively would be appreciated! Any ideas how to fix this? Any advice would be much appreciated. My team is using Javascript for this application.