0

I'm running the below fetch request on an array of items, they all fail expect the last one. It doesn't matter if the number of items are 10, 100, only the last item in an array will succeed in the fetch request.

var issns = ['09295666', '08989621', '02365294', '03643107', '02365294', '00015970', '03038173'];
issns.forEach((issn) => {
  goFetchBoy(issn);
});

async function goFetchBoy(issn) {
  var postresponse = await fetch('https://proquestFake.com/api/1-0-0/issnsearch', {
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      isxn: isxn,
      resultsPerPage: 20,
    }),
  });
  var data = await postresponse.json();
  console.log(data);
}

Same code when tried in ChromeExtension>background script: enter image description here Same code when tried in Devtools>console.log : enter image description here

As can be seen in the screenshot from devtools, the array has a value. Note: I also rewrote this code in Ajax.Jquery and had the exact same issue.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
jimidoors
  • 1
  • 3
  • 1
    It happens because Devtools evaluates object preview lazily, see [Weird behavior with objects & console.log](https://stackoverflow.com/questions/23429203/weird-behavior-with-objects-console-log) – wOxxOm Mar 14 '20 at 11:40
  • is there a way i can make my async function as lazy as devtools so i can work with the Array(1) value in my 2nd screenshot? – jimidoors Mar 14 '20 at 12:36
  • Try the suggestions in that topic e.g. console.log(JSON.parse(JSON.stringify(data))) – wOxxOm Mar 14 '20 at 12:38
  • I just did that and still the same.. The problem isn't with console.log. I understand the console loads lazily, which is good for my case. But, i am unable to make the async function in my background script to load as lazily as console.log. Have I missed something in the async function, which looks like its resolving the fetch request too soon? – jimidoors Mar 14 '20 at 12:46
  • Make sure you've reloaded the extension on chrome://extensions page after editing the background script as it's a common pitfall. – wOxxOm Mar 14 '20 at 12:58
  • I reloaded the ext. and even tried with a new list of array and Still the same results,. Screenshot: https://i.imgur.com/uIb9jxY.png All the items in the array resolves too soon, except the last one. Can you suggest any other alternative? – jimidoors Mar 14 '20 at 13:13
  • Sounds like we were going in the wrong direction all along. The proper solution should start with a proper investigation so verify the responses of those requests in devtools network panel and if there's really nothing in the array then try to find what's different to the request in devtools, like a header or a parameter, whatever. – wOxxOm Mar 14 '20 at 13:38

0 Answers0