我有一个conditional fetch来确定后续URLfetch的一部分,但我只希望它在某些条件下运行。

以下内容不等待并且立即运行try

async function fetchURLs() {
let sessionWait = false;
  if ((check1 === true) && (check2 === false))
        {sessionWait = await getURL();
        } else {sessionWait = true}

  if (sessionWait === true){
  try {
    var [a, b, c] = await Promise.all([

      fetch(dataUrl, {
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        method: 'post',
      }).then((response) => response.text()).catch(error => console.log(error.message)),
      fetch(settingsUrl).then((response) => response.text()).catch(error => console.log(error.message))

  } catch (error) {
    console.log(error);
  }

});
}

async function getURL(){

   let subDomain = 'a';

   fetchAdd = "https://" + subDomain + ".dexcom.com/ShareWebServices/Services/General/LoginPublisherAccountByName"
  await fetch(fetchAdd, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
   }).then((res) => res.json()).then((SessionData) => dataUrl = "https://" + SessionData).catch(error => console.log(error.message));

 return true;
}

最佳答案

 const tasks = getTaskArray();
 return tasks.reduce((promiseChain, currentTask) => {
 return promiseChain​.​then​(​chainResults =>
    currentTask​.​then​(​currentResult =>
        [ ...chainResults, currentResult ] )
  );
  }, Promise​.​resolve​([])).​then​(​arrayOfResults => {
  // Do something with all results
 });


此链接可能会对您有所帮助。
    https://decembersoft.com/posts/promises-in-serial-with-
    数组减少/

关于javascript - 如何在Promise.all中获取访存以等待另一个有条件的访存语句的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58507796/

10-11 17:25