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
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
Share Improve this question asked Dec 2, 2018 at 13:36 Marwan Al-MasaeidMarwan Al-Masaeid 1241 silver badge10 bronze badges 2- Maybe this is what you're looking for stackoverflow./questions/9121902/… – Guillermo Aguirre Commented Dec 2, 2018 at 13:38
- This is simply not possible. You need to change the way it is called. – Bergi Commented Dec 2, 2018 at 13:56
1 Answer
Reset to default 5You 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)
})()