First of all I apologize for asking question about async/await, as I know there are lots of answers, however after reading many of them, I have not come to a working solution.
There is a search function flickr.js which searches Flickr API for a query, but that function returns Promise, but I need an object {query, images : [{photo1}, {photo2}, {photo3}]}
flickr.js function
window.MODULES.Flickr = async query => {
const opts = {
api_key: 'API_KEY'
};
const photos = await window.fetch(
`https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=${
opts.api_key
}&text=${query}&format=json&nojsoncallback=1
`
);
const data = await photos.json();
const pictures = data.photos.photo;
const images = [];
pictures.forEach(picture => {
return images.push({
id: picture.id,
url: `https://farm${picture.farm}.staticflickr.com/${picture.server}/${
picture.id
}_${picture.secret}_s.jpg`,
title: picture.title
});
});
return {
query,
images
};
};
and it is called from
ImageFinder.js
function() {
let ImageFinder = (window.CLASSES.ImageFinder = function() {});
ImageFinder.prototype.search = (query, moduleId) => {
switch (moduleId) {
case 'static':
return window.MODULES.Static(query);
case 'flickr':
return window.MODULES.Flickr(query); // Need to return object { query, images : [{photo1}, {photo2}, {photo3}] }
default:
throw Error('Search module not found.');
}
};
});