最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how I can call promise synchronously - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 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)
})()

发布评论

评论列表(0)

  1. 暂无评论