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