-4

I have a function making http request, like this

function waitforme(para){
    fetch(para).then( return false/true; )
}

if(waitforme == false){
} else {}

now it is not waiting for waitforme function response and execute the rest. How can i make it wait for the response and then excute the rest ?

atul1039
  • 143
  • 4
  • 13
  • Check the documentation to use fetch with async/await: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch – Camilo May 19 '21 at 17:56

1 Answers1

0

If you need to wait, you can await your async response.

const waitForMe = async (para) =>
  fetch(para)
    .then(() => true)
    .catch(() => false);

const main = async () => {
  const response = await waitForMe('some-url');
  if (response) {
    console.log('Success');
  } else {
    console.log('Failure');
  }
};

main();
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132