同步调用Async函数
//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});
});
通话功能
if(SyncValue()){
//here do the logic
}
如何锁定函数调用以从“ syncValue”函数中检索值
答案 0 :(得分:2)
您可以在IIFE中使用async/await
语法,在其中等待异步调用解析然后使用该值。
function asyncCall() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(5)
}, 2000)
});
}
(async function() {
const result = await asyncCall();
if (result) console.log(result)
})()