1

My function looks like this now:

var GetImages = async() => {
var images_array = [];
await request ({
    url: `https://api.tumblr.com/v2/blog/nameblog/posts?api_key=${process.env.TUMBLR_KEY}&type=photo`,
    json: true
}, (error, response, body) => {
    if(error){
        console.log('Unable to connect');
    }else if(body.meta.status === "ZERO_RESULTS"){
        console.log('Uable to find that address.');
    }else if(body.meta.status === 200){
        body.response.posts.forEach(function(obj) {
            obj.photos.forEach(function(photo) {
                if(photo.original_size.width>photo.original_size.height){
                    images_array.push(photo.original_size.url);
                    console.log("dawdaw");
                } 
            });
        });
        //callback(images_array);
    }
});
 return images_array;
}

I have no idea, how return my array after i'll fill it with values. With callback it works fine, but i wanna do it with async/await methid in right way. Thank you for help.

Oleksandr
  • 137
  • 9
  • 2
    You need to _promisify_ `request` or use `request-promise` module. – alexmac Aug 12 '17 at 20:02
  • First off, you need to use a version of `request` like `request-promise` that returns a promise because `await` only does async stuff with promises. Then, you need to return a promise from your async function for it's caller to be able to use its result. You can't just use `await` to somehow synchronously return an async result. `await` does some magic, but not that kind of magic. Your `GetImages()` function needs to return a promise too. – jfriend00 Aug 12 '17 at 20:49

1 Answers1

0

create method to return promise for request and use that method with await

requestPromise = () => {

    return new Promise(function(resolve, reject) {
        request({
            url: `https://api.tumblr.com/v2/blog/nameblog/posts?api_key=${process.env.TUMBLR_KEY}&type=photo`,
            json: true
        }, (error, response, body) => {
            if (error) {
                console.log('Unable to connect');
                reject();
            } else if (body.meta.status === "ZERO_RESULTS") {
                console.log('Uable to find that address.');
                reject();
            } else if (body.meta.status === 200) {
                body.response.posts.forEach(function(obj) {
                    obj.photos.forEach(function(photo) {
                        if (photo.original_size.width > photo.original_size.height) {
                            images_array.push(photo.original_size.url);
                            console.log("dawdaw");
                        }
                    });
                });
                resolve(images_array)
            }
        });
    });

}

var GetImages = async() => {
    try
    {
         images = await requestPromise();
         return images;
    }
    catch(e){return [];}
}
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
  • You should promisify only `request`, and put all the stuff from the callback inside the `GetImages` function. – Bergi Aug 12 '17 at 21:50