async function getTransactions(i) { let response = await // fetch here let data = await response.json(); return data; } function process(r, i, a) { console.log(i) result.push(r[0].data.recipeSearch.result) if (i == a){ //* 1 + 7))) { console.log("download") //download("ah.json", result); } } function download(filename, text) { var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(text))); pom.setAttribute('download', filename); if (document.createEvent) { var event = document.createEvent('MouseEvents'); event.initEvent('click', true, true); pom.dispatchEvent(event); } else { pom.click(); } } result = [] a = 2//217 for (let index = 0; index <= a; index++) { await getTransactions(index).then(data => process(data, index, a)); }