0
function apiCall() {
  return new Promise(
    (resolve) => {
      setTimeout(() => resolve({ data: { test: "123" } }), 1000);
    },
    (reject) => {},
  );
}

async function caller() {
  return await apiCall().then((res) => {
    res.data;
  });
}

console.log(caller());

This call returns a pending promise. How can I wait in the caller function until the promise is ready and then return the value? I don't want to call await caller()

AKX
  • 152,115
  • 15
  • 115
  • 172
Anton
  • 19
  • 3
  • async functions return promise. you have to either await or use .then() – Janar Jan 08 '22 at 23:29
  • You can wait *inside* the `caller` function, but that doesn't make `caller()` synchronous - it's still an asynchronous timeout, and it's impossible to immediately get a value from the future, it will always return a promise. No way around, your `console.log` has to wait for it like everyone else. – Bergi Jan 08 '22 at 23:30

0 Answers0