result.forEach(function(items) {
request.get("https://www.googleapis.com/youtube/v3/search?part=snippet&order=viewCount&q="+items['song']+'+'+items['song']+"&type=video&key=AIzaSyAvDAdEnqrStOJNnpnGy9BkrC_sG-gcHIU", function(err,res,body){
if(res.statusCode == 200 ) {
console.log(JSON.parse(body)['items'][0].id.videoId);
}
});
});
let promise = Promise.resolve();
for(...) {
promise.then( () => {
return new Promise((resolve) => {
request.get(..., (data) => {... resolve(data);});
}
}
}
promise.then((lastData) => {...});
function* code(result) {
for(var i = 0; i < result.length; i++) yield result[i];
}
function code2(gen) {
items = gen.next().value;
request.get("https://www.googleapis.com/youtube/v3/search?part=snippet&order=viewCount&q="+items['song']+'+'+items['song']+"&type=video&key=AIzaSyAvDAdEnqrStOJNnpnGy9BkrC_sG-gcHIU", function(err,res,body){
if(res.statusCode == 200 ) {
console.log(JSON.parse(body)['items'][0].id.videoId);
code2(gen);
}
});
}
let gen = code(result);
code2(gen);
npm i async --save
var items = []; //здесь ваш массив
async.eachSeries(items, function(item, callback) {
request.get("https://www.googleapis.com/youtube/v3/search?part=snippet&order=viewCount&q="+item['song']+'+'+item['song']+"&type=video&key=AIzaSyAvDAdEnqrStOJNnpnGy9BkrC_sG-gcHIU", function(err, res, body){
if(res.statusCode == 200 ) {
console.log(JSON.parse(body)['items'][0].id.videoId);
callback();
} else {
callback('Error');
}
});
}, function(err) {
if( err ) {
console.log('Ошибка случилась');
} else {
console.log('Все хорошо едем дальше');
}
});