JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<textarea id='some-text-area' rows="20" cols="80"></textarea>
JavaScript
//example array to loop.
var queries= ['query1', 'query2', 'query3', 'query4'];
//the textarea where im going to print the results in order later to open in excel as a .csv file
var $csvText= $('#some-text-area');
//inserting the csv headers
$csvText.val('Column1, Column2\r\n');
queries.reduce(function(p, item) {
return p.then(function() {
return someGoogleAPI(item).then(function(result) {
console.log(JSON.stringify(result));
var prev = $csvText.val();
$csvText.val(prev + JSON.stringify(result) + '\r\n');
});
});
}, Promise.resolve());
function someGoogleAPI(val) {
return new Promise(function(resolve) {
setTimeout(function() {
resolve({column1: val + ' column1', column2: val+' column2'});
}, Math.floor((Math.random() * 1000) + 1));
});
}