function fetchData(): Promise<string> {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve("Data fetched!");
        }, 2000);
    });
}

async function getData() {
    const result = await fetchData();
    console.log(result);
}

getData(); // After 2 seconds, Output: Data fetched!
Bash

Explanation:

  • This program uses the async/await syntax to handle asynchronous operations.
  • The fetchData function returns a promise that resolves after 2 seconds.
  • The getData function waits for the promise to resolve and then logs the result.
Resize text
Scroll to Top