0

I don't know about the correct title for my problem. I just need a value going out of a function, just like return but I think it's not same. i have code snippet from controller in adonisjs framework:

var nmeadata = "";
jsondata.forEach(element => {
    var x = element.nmea
    var buff = new Buffer(x, 'base64')
    zlib.unzip(buff, (err, res) => {
        if(err)
        {
            //
        }
        else
        {
            nmeadata += "greed island"
            nmeadata += res.toString()
        }
    })
});

return view.render('admin.index', {
    data: datanmea.toJSON(),
    nmea: nmeadata
})

I need the result of unzipped string data that inserted to nmeadata from zlib function then send it to view. But, for this time, even I cannot displaying a simple output like greed island to my view.

thank you.

UPDATE

Still not working after using promises:

class NmeaController {

    async index({view})
    {
        
        const datanmea = await NmeaModel.all()
        const jsondata = datanmea.toJSON()

        var promises = [];
        var nmeadata = "";
        jsondata.forEach(element => {
            promises.push(
                new Promise(resolve => {
                    let x = element.nmea
                    let buff = new Buffer(x, 'base64')
                    zlib.unzip(buff,
                        (err, res) => {
                            if (err) {
                            //
                            } else {
                            nmeadata += "test add text"
                            // nmeadata += res.toString()
                            }
                            //im also try using resolve() and resolve("any text")
                            resolve(nmeadata);
                        })
                    }
                )
            )
        });
        await Promise.all(promises);
        return view.render('admin.index', {
            data: datanmea.toJSON(),
            nmea: nmeadata
        });
    }

UPDATE AUGUST 22 2019

i'm already tried solution from maksbd19 but still not working

class NmeaController {

    async index({view})
    {
        
        const datanmea = await NmeaModel.all()
        const jsondata = datanmea.toJSON()

        var promises = [];
        var nmeadata = "";
        jsondata.forEach(element => {
            promises.push(
                new Promise(resolve => {
                    let x = element.nmea
                    let buff = new Buffer(x, 'base64')
                    zlib.unzip(buff,
                        (err, res) => {
                            if (err) {
                                // since you are interested in the text only, so no need to reject here
                                return resolve("");
                            }
                            return resolve("greed island")
                        })
                    }
                )
            )
        });
        const result = await Promise.all(promises); // this will be an array of results of each promises respectively.
        nmeadata = result.join(""); // process the array of result 
        return view.render('admin.index', {
            data: datanmea.toJSON(),
            nmea: nmeadata
        });
    }
}

Community
  • 1
  • 1
dhanyn10
  • 704
  • 1
  • 9
  • 26
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – CertainPerformance Aug 05 '19 at 08:10
  • 2
    Convert the callback API to promises, then call `Promise.all` on an array of promises – CertainPerformance Aug 05 '19 at 08:11
  • @CertainPerformance can you give me a example? – dhanyn10 Aug 16 '19 at 04:20
  • async function foo() { var url= 'https://jsonplaceholder.typicode.com/todos/1'; var result= await (await fetch(url)).text(); // or .json() return result; } async function load() { var result = await foo(); console.log(result); } load(); – putra irawan Aug 19 '19 at 12:39
  • Try to add await when converting datanmea to JSON: const jsondata = await datanmea.toJSON() – asubanovsky Aug 20 '19 at 15:34

1 Answers1

0

I'd suggest two things-

  1. modify zlib.unzip callback function to resolve properly;
(err, res) => {
    if (err) {
        // since you are interested in the text only, so no need to reject here
        return resolve("");
    }
    return resolve(res.toString())
}
  1. retrieve the final data from the result of Promise.all
const result = await Promise.all(promises); // this will be an array of results of each promises respectively.
nmeadata = result.join(""); // process the array of result 

In this approach every promise will resolve and finally you will get the expected result in array.

maksbd19
  • 3,785
  • 28
  • 37