Search code examples
javascriptnode.jsjsonasync-awaitinsomnia

How to implement async / await


I would like to know how do I return a new array of objects in response.json () to be visible in insomnia.

I am receiving various information from an API and the goal is to return to my frontend just an array of objects within the response.json () created by me.

So I want to store them inside a variable.

This is the code:

    var T = new Twit({
        consumer_key: process.env.CONSUMER_K,
        consumer_secret: process.env.CONSUMER_S,
        access_token: process.env.ACCESS_T,
        access_token_secret: process.env.ACCESS_T_S,
        timeout_ms: 60 * 1000,
        strictSSL: true,
})

app.get('/teste', (request, response) => {

let obj = {};
let obj2 = {};

    T.get('https://******.com/1.1/statuses/user_timeline.json?screen_name=******=1', (err, data, response) => {

        data.map(item => {
            obj.name = item.user.name;
            obj.texto = item.text;
            obj.profileImage = item.user.profile_image_url;
        });
    });
    T.get('https://******.com/1.1/statuses/user_timeline.json?screen_name=u*******=1', (err, data, response) => {
    
        data.map(item => {
            obj2.name2 = item.user.name;
            obj2.texto2 = item.text;
            obj2.profileImage2 = item.user.profile_image_url;
        });
    
    });   

    setTimeout(() => {
      return response.json([obj, obj2]);
    }, 1000);
    
});

With the help of the people responsible for me, to be able to return a cleaner json, but now I would like to know how to implement aync / await in place of "setTimeout ()", as the function only returns an array when using the "dalay" of "setTimeout ()".

Thanks!


Solution

  • The problem is your array variable is out of the route function, making it global which means that the previous array values are stored even when you create a new GET request. Hence you can either add the array variable inside the route function or you can empty the array as soon as the GET request is made.

    An example:

      var T = new Twit({
                consumer_key: process.env.CONSUMER_K,
                consumer_secret: process.env.CONSUMER_S,
                access_token: process.env.ACCESS_T,
                access_token_secret: process.env.ACCESS_T_S,
                timeout_ms: 60 * 1000,
                strictSSL: true,
        })
        
        app.get('/teste', (request, response) => {
            var obj = {};
            T.get('https://******.com/1.1/statuses/user_timeline.json?screen_name=******=1', (err, data, response) => {
        
                data.map(item => {
                    obj.name = item.user.name, 
                    obj.texto = item.text 
                    obj.profileImage = item.user.profile_image_url
                });
        
            });
            T.get('https://******.com/1.1/statuses/user_timeline.json?screen_name=u*******=1', (err, data, response) => {
            
                data.map(item => {
                    obj.name2 = item.user.name, 
                    obj.texto2 = item.text 
                    obj.profileImage2 = item.user.profile_image_url
                });
            
            });    
        
            return response.json([obj]);
        });