Sobes.tech
Junior — Middle

How to implement waiting for the completion of an asynchronous process and obtaining its result?

sobes.tech AI

Answer from AI

In Node.js, to wait for an asynchronous process to complete and get the result, promises and async/await are usually used. For example, if there is a function that returns a promise, you can wait for its result like this:

async function getData() {
  try {
    const result = await asyncProcess();
    console.log('Result:', result);
  } catch (error) {
    console.error('Error:', error);
  }
}

getData();

Here, asyncProcess is an asynchronous function that returns a promise. The await keyword pauses execution until the result or an error is received.