【问题标题】:Inserting a Method from async.js in to my code将 async.js 中的方法插入到我的代码中
【发布时间】:2018-08-15 16:44:56
【问题描述】:

所以我正在编写一个代码,它将数据名称、ID 等从服务器导出到 Excel 表中的用户的浏览器。

现在我正在使用 NodeJS 进行异步编程,有时服务器可能会在所有数据导出到其中之前使用 Excel 表发送响应。

我的同事告诉我,他们使用 .async 包。 Here is the Link to it. 我认为我们应该使用这种方法。

现在这是我的代码:

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

我的问题是我不完全知道如何在方法中实现我的代码以使其工作,因为异步包对我来说似乎很复杂。

【问题讨论】:

标签: javascript node.js asynchronous async.js


【解决方案1】:
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);
});

【讨论】:

    猜你喜欢
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 2021-12-04
    相关资源
    最近更新 更多