0

I've been all over looking at answers but I need some help. I'm trying to emulate Combination of async function + await + setTimeout

while (goOn) {
  // other code
  var [parents] = await Promise.all([
                    listFiles(nextPageToken).then(requestParents),
                    timeout(5000)
                  ]);
   // other code
}

The reason is that I am looping the values in a grid and sending it to a NIH url. I can only send 10 requests in a second. So my first attempt wait to use await and async to not send a request until done. Well that is too fast also. So I would like to put 1/10th of a second sleep time between each request. And I can't get anything to work.

my code is

async function myFunction5() {
  var datainformation = $('#grid').jqxGrid('getdatainformation');
  var rowscount = datainformation.rowscount;
  try {
    for (let i = 0; i < rowscount; i++) {
      var data = $('#grid').jqxGrid('getrowdata', i); //
      let currentTerm = "(" + Strings.orEmpty(data['First Name']) + " " +
        Strings.orEmpty(data['Middle Initial']) + " " + Strings.orEmpty(data['Last Name']) + " " +
        Strings.orEmpty(data['Suffix']) + "[Author - Full]) AND " + Strings.orEmpty(data['Affiliations']) +
        "[Affiliation]";

      let terms = encodeURI(currentTerm);          
      const url =
        "/path/to/resource/" +
        terms;         

      var [pmids_List] = await Promise.all([
        getJson(url, i),
        timeout(100)
      ]);
    }
    //alert('all done');
  } catch (err) {
    alert(i + " " + data['Last Name']);
    console.log('done with some errors');
  }
}

and it calls

function getJson(url, i) {
  var timestamp = new Date().toJSON();
  console.log(timestamp + ' getJSON')
  return new Promise(function (resolve, reject) {
    $.ajax({
      url: url,
      dataType: 'json',
      crossDomain: true,
      success: function (data) {
        var pmids = data.esearchresult.idlist;
        var pmidlist = pmids.join();
        pmid_List.push(pmidlist);
        console.log('success', i)
      },
      error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr.status);
        console.log(thrownError);
      },
      complete: function (a, b) {
        resolve(a);
        console.log('complete', i);
      }
    });
  });  
}

I know that my Promise.all is wrong but I can't figure out what is correct. Your help is appreciated. And if you have a better idea -I'm all ears. Also I need to know how to determine when the loop has totally completed so I can go onto the next step

guest271314
  • 1
  • 15
  • 104
  • 177
Bill
  • 1,423
  • 2
  • 27
  • 51
  • 4
    Please provide more detail than "*I can't get anything to work.*" - the code looks good, it should work. – Bergi Feb 18 '19 at 16:16
  • 1
    what is your `timeout` function? – Marius Feb 18 '19 at 16:16
  • you should take you api key and stuff like that from your example – Eli Feb 18 '19 at 16:21
  • _"can only send 10 requests in a second."_ What is the `.length` of `listFiles` and `rowscount`? – guest271314 Feb 18 '19 at 16:22
  • What is the expected result of `pmids_List` at `var [pmids_List] = await Promise.all([ getJson(url, i), timeout(100) ])`, where the value is overwritten at each iteration of the `for` loop though `pmid_List.push(pmidlist)` appears within `success` of `$.ajax()`? Or is the issue the delay between request; or the delay not being suffient itself? – guest271314 Feb 18 '19 at 16:29
  • 1
    OK- sorry that I missed some code-- been working on this problem all night because the wrong error message would come back from the NIH - so yes I was missing function timeout(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } Once I added that and set the time out to 150 it works-- Sorry I jumped the gun – Bill Feb 18 '19 at 16:33
  • Why is `pmids_List` being overwritten within `for` loop? – guest271314 Feb 18 '19 at 16:34
  • list file are about 10 and the rowcount can be over 100- it depends on what is being asked – Bill Feb 18 '19 at 16:34
  • Thanks for taking out the api_key-- been up for many many hours- and totally messed up – Bill Feb 18 '19 at 16:36
  • @user1314159 No worries. If there is no problem with the code you can delete the question yourself. – guest271314 Feb 18 '19 at 16:37
  • The PMID_List will have the ID's of all of the members of a medical institute. I will then pull out the duplicates so I can identify a publication that has a local co-author – Bill Feb 18 '19 at 16:37
  • Thanks all-- yes I have the wrong PMID_List array being used and that is now changed and thanks Bergi for your original code – Bill Feb 18 '19 at 16:43
  • You should take it 1 step further and revoke that API key and generate a new one because its still visible in the edits for anyone to see. – Andrew Lohr Feb 18 '19 at 17:15

0 Answers0