0

So I am working on a code which exports Data Names, IDs etc. from a Server to the Browser from the User in a Excel Sheet.

Now as I am programming asychronous with NodeJS, it could be that sometimes the Server will send the response with the Excel Sheet, before all the data could be exported into it.

My workmates told me, that they use the .async Package for it. Here is the Link to it. I think we should be using this method.

Now this is my code:

appDataIDList.forEach((appDataID, index) => {
    appData.getByID(appDataID.id).then((appData) => {
        ws.cell(index + 1,1).string(appData.name).style(style);
        callback();
    })
});
wb.write('ExcelReport.xlsx', res);

My Problem is that I do not exactly know how to implement my code in to the method to make it work, because the async Package seems complex to me.

Murut
  • 3
  • 2
  • Possible duplicate of [async.eachSeries in node.js](https://stackoverflow.com/questions/23864052/async-eachseries-in-node-js) – msanford Mar 07 '18 at 14:33

1 Answers1

0
var async = require('async');

async.eachOf(appDataIDList, (appDataID, index, callback) => {
    appData.getByID(appDataID.id).then((appData) => {
        ws.cell(index + 1,1).string(appData.name).style(style);
        callback();
    });
}, (err) => {
    // This is a callback function that is called when the 
    // first error is received or everything's finished.
    // We could catch an error here, if callback('random error') is called
    // then err = 'random error'
    wb.write('ExcelReport.xlsx', res);
});
Timo
  • 727
  • 5
  • 15