首页 > 解决方案 > 从 js.node 中的异步函数导出值

问题描述

我有一个在函数外部声明并添加到 fileActions 函数内部的列表。在我的代码运行完成后,我想在 modules.exports 中导出列表。如果我在 file2.on('finish', function() 下输出列表,我会得到我想要的结果。我的问题是我不知道如何将列表从异步函数中取出并放入 modules.exports。

function getFile(){
    let file = fs.createWriteStream(getPath()+"file.json");
    request = https.get("https://fileLocation.com/file.json", function(response) {   
        response.pipe(file);
        file.on('finish', function() {
            fileActions();
            file.close();
        })
    });
}

代码块将在方法映射内,并将从

module.exports = {
    call: function (method, param, response) {

        if (methodMap[method]) {
            const result = methodMap[method](param, response);
            omnis_calls.sendResponse(result, response);
            return true;
        }
        else {
            throw Error("Method '" + method + "' does not exist");
        }

    }
};

标签: javascriptnode.jsasynchronous

解决方案


推荐阅读