简体   繁体   English

从async.js将方法插入到我的代码中

[英]Inserting a Method from async.js in to my code

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. 因此,我正在编写一个代码,该代码将服务器中的数据名称,ID等从用户导出到Excel工作表中的用户。

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. 现在,当我与NodeJS进行异步编程时,可能有时服务器会在将所有数据导出到其中之前,将其与Excel工作表一起发送响应。

My workmates told me, that they use the .async Package for it. 我的同事告诉我,他们使用.async软件包。 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. 我的问题是我不完全知道如何在方法中实现我的代码以使其工作,因为异步包对我来说似乎很复杂。

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM