I'am passing an array with images titles and I need google to search each title and return me it's image link. When google returns a link I push it to an empty array to get all the links for my images. I don't know how could I return the array with links, because as it is now the script olny returns an empty array, it probably because the program executes return before the array is filled? So in summary I need to return array from this function. By the way if I console.log(as) in every iterration of foreach I get the array with links, so google API returns the link.
const google = require('googleapis').google
const customSearch = google.customsearch('v1')
async function paieska (placestitles) {
let as = [];
const error = "Image not found";
placestitles.forEach(async title => {
const response = await customSearch.cse.list({
auth: 'MY_API_KEY',
cx: 'MY_CX',
q: title,
num: 1,
searchType: 'image',
imgSize: 'medium'
})
if(response.data.items) {
response.data.items.map((item) => {
as.push(item.link);
});
}
else {
as.push(error);
}
});
return as;
}