I am making a call to Udemy API. To make simultaneous calls, I am using a loop. By doing so, I am automatically incrementing the page numbers and trying to fetch the data from each page and store it into an array so that I can write all the data into a single file in json format. But all I am getting is an empty array. How do I access the value returned by the promise and store into the doc.table array?
My code:
const fetch=require("node-fetch");
const fs=require("fs");
let doc={};
doc.table=[];
for(let i=1;i<=10;i++){
fetch('https://www.udemy.com/api-2.0/courses/ page='+i+'&page_size=10&client_id=${client_id}&client_secret=${client_secret},{
method:'GET',
body:null,
headers:{authorization: ${auth_code}}
})
.then(res=>res.json())
.then(json=>doc.table.push(json))
};
fs.writeFile("UDEMY.json",JSON.stringify(doc),function(err){
if(err) throw err;
console.log("Complete");
});