仅在某些承诺解决后才导入/导出 [英] Importing / exporting only after certain promises have resolved

查看:99
本文介绍了仅在某些承诺解决后才导入/导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含某些承诺的文件,按顺序执行时,准备输入文件 input.txt

Let's say I have a file containing certain promises, that when executed in order, prepares an input file input.txt.

// prepareInput.js

var step1 = function() {
    var promise = new Promise(function(resolve, reject) {
        ...
    });
    return promise;
};
              
var step2 = function() {
    var promise = new Promise(function(resolve, reject) {
        ...
    });
    return promise;
};
              
var step3 = function() {
    var promise = new Promise(function(resolve, reject) {
        ...
    });
    return promise;
};

step1().then(step2).then(step3);

exports.fileName = "input.txt";

如果我运行节点prepareInput.js ,行 step1()。然后(步骤2)。然后(步骤3)执行并创建文件。

If I run node prepareInput.js, the line step1().then(step2).then(step3) gets executed and creates the file.

如何更改此项以便在其他文件尝试检索时来自此模块的 fileName step1()。然后(步骤2)。然后(步骤3); 运行并完成之前fileName是暴露的吗?以下内容:

How can I alter this so that when other files attempt to retrieve fileName from this module, step1().then(step2).then(step3); is run and completed before fileName is exposed? Something along the line of:

// prepareInput.js
...

exports.fileName = 
    step1().then(step2).then(step3).then(function() {
        return "input.txt";
    });


// main.js
var prepareInput = require("./prepareInput");
var inputFileName = require(prepareInput.fileName);
              

Node.js初学者在这里;事先道歉,如果我的方法完全没有意义......:)

Node.js beginner here; apologize beforehand if my approach makes completely no sense... :)

推荐答案

你不能直接导出检索到的东西的结果异步,因为导出是同步的,所以它在检索任何异步结果之前发生。这里通常的解决方案是导出一个返回promise的方法。然后调用者可以调用该方法并使用该promise来获得所需的异步结果。

You can't directly export the results of something retrieved asynchronously because export is synchronous, so it happens before any async results have been retrieved. The usual solution here is to export a method that returns a promise. The caller can then call that method and use that promise to get the desired async result.

module.exports = function() {
    return step1().then(step2).then(step3).then(function() {
        // based on results of above three operations, 
        // return the filename here
        return ...;
    });
}

然后来电者这样做:

require('yourmodule')().then(function(filename) {
    // use filename here
});






要注意的一点是,如果有的话在一系列事物中的操作是异步的,然后整个操作变为异步,然后没有调用者可以同步获取结果。有些人以这种方式将异步称为传染性。因此,如果您的操作的任何部分是异步的,那么您必须为最终结果创建一个异步接口。


One thing to keep is mind is that if any operation in a sequence of things is asynchronous, then the whole operation becomes asynchronous and no caller can then fetch the result synchronously. Some people refer to asynchronous as "contagious" in this way. So, if any part of your operation is asynchronous, then you must create an asynchronous interface for the eventual result.

你也可以缓存承诺,因此每个应用程序只运行一次:

You can also cache the promise so it is only ever run once per app:

module.exports = step1().then(step2).then(step3).then(function() {
    // based on results of above three operations, 
    // return the filename here
    return ...;
});

然后来电者这样做:

require('yourmodule').then(function(filename) {
    // use filename here
});

这篇关于仅在某些承诺解决后才导入/导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆