sequential request with synchronous request with
Using Rest Api get method called sequential request of settimeout on 1 sec. after each request data massaging and put in array. Now I would like to get this array after all request were done.
async function sequentialMF(mfIds) {
var mfData = [];
try {
var timeout = 0;
return await Promise.all(mfIds.map(mfId => {
console.log(mfId)
var options = {
//https://www.quandl.com/api/v3/datasets/AMFI/103504.json?api_key=WfUR65SA5p1PzpBysgK4
method: 'GET',
url: 'https://www.quandl.com/api/v3/datasets/AMFI/' + mfId + '.json',
qs: { api_key: 'WfUR65SA5p1PzpBysgK4' },
};
setTimeout(function () {
request(options, function (error, response, body) {
var obj = JSON.parse(body);
var mfobj = {
"code": obj.dataset.dataset_code,
"name": obj.dataset.name,
"date": obj.dataset.end_date,
"nav": obj.dataset.data[0][1]
}
mfData.push(mfobj);
console.log(obj.dataset.dataset_code + ' ' + obj.dataset.data[0][1]);
console.log('lngth ' + mfData.length + ' ' + mfIds.length)
})
}, timeout);
timeout += 1000;
new Promise(function (resolve, reject) {
if (mfData.length === mfIds.length) {
console.log('testing')
resolve(mfData);
} else reject('errrrrrrrrrrrrror')
})
}))
} catch (err) {
console.log(err)
}
}