0

I'm working with some fairly basic scripts for two specific scenarios, one GET and one POST fetch (These are being run in the Edge developer console if that matters for anything it's the only option unfortunately).

For the GET, it works fine because there's no importance to which one gets processed first:

var arr = ['12345', '12534', '53435'];
arr.forEach((singleData) => {
    const test = fetch(`https://COMPANY-LAN/apps/StdAction.do?actionId=100&workflowId=${singleData}`, {
  "headers": {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-language": "en-US,en;q=0.9",
    "sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Microsoft Edge\";v=\"97\", \"Chromium\";v=\"97\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "cross-site",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1"
  },
  "referrer": "https://COMPANY-LAN/apps/StdAction.do",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": "null",
  "method": "GET",
  "mode": "cors",
  "credentials": "include"
});
console.log(`Fetch for ${singleData} is complete`);
})

However, for the POST it's not working at all - I presume because it needs to complete the first item and then move on to the second one. As it's written, it runs through it but only the first item in the array gets processed with multiple POST's instead of once for each item in the array:

var arr = ['12345', '12534', '53435'];
arr.forEach((singleData) => {
    const test = fetch("https://COMPANY-LAN/apps/StdAdction.do", {
  "headers": {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-language": "en-US,en;q=0.9",
    "sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Microsoft Edge\";v=\"97\", \"Chromium\";v=\"97\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "cross-site",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1"
  },
  "referrer": `https://COMPANY-LAN/apps/StdAction.do?actionId=200&workflowId=${singleData}`,
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": "method=processApp&appRemarkString=Application+processed+by+script.",
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});
console.log(`Fetch for ${singleData} is complete`);
})

I'm not all that familiar with async, would this be the better option? Or some other approach? Would appreciate any feedback. Also, as far as sequence goes - I'll be running the POST first for the entire array, and once that is complete and there's no issues, then I'll run the GET for the entire array (The real-world use will involve about 40-50 items in the array at a time).

Thanks.

  • "*would this be the better option? Or some other approach?*" What constitutes "*better*" in any scenario with zero parameters as to how *exactly* you would measure it is largely a subjective exercise, questions of which type are explicitly off-topic per the scope of the site defined in the [help/on-topic]. – esqew Feb 15 '22 at 21:27
  • 1
    Yes, learn about promises and then use `async`/`await`. – Bergi Feb 15 '22 at 21:30
  • 1
    I don't think 'better' is subjective in this case, your current approach doesn't work so anything that works could be considered better. You are right that you need to wait for the POST to complete, and using `await` is probably the way to go. There's an abundance of tutorials for that online though. [async await in for loops](https://zellwk.com/blog/async-await-in-loops/) – picklepick Feb 15 '22 at 21:33
  • @picklepick - Thank you, yes I was just trying to make sure I was going to start down the correct path this time. Appreciate your help, despite the poor wording in my post. – LetThatSinkIn Feb 15 '22 at 22:03

0 Answers0