5

calling an Async function synchronously

//return Boolean value
function SyncValue(){ 
var promise = new Promise((resolve, reject) => {
    if(/* asynchronous code execution is successful */) {
     return true;
    } else {
     return false;
    }
    return promise.then(returnValue =>{ return returnValue});
});

call function

if(SyncValue()){
   //here do the logic 
}

how I can lock the calling for the function to retrieve a value from "syncValue" function

1 Answers1

5

You could use async/await syntax with IIFE where you wait for your async call to resolve and then use that value.

function asyncCall() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(5)
    }, 2000)
  });
}

(async function() {
  const result = await asyncCall();
  if (result) console.log(result)
})()
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176