简体   繁体   English

向承诺链添加其他方法

[英]Add addional method to promise chain

I use the following code which works OK,Currenlty the pars.getEx is returning a string to run to the processExe method,Now I need somehow to pass also port from other function to processExe ,how can I do that ? 我使用下面的代码可以正常工作,cursollty the pars.getEx返回一个字符串以运行到processExe方法,现在我需要以某种方式将其他函数的端口也传递给processExe ,我该怎么做?

  var portscanner = require('portscanner');
  var Promise = require('bluebird');
  var fs = Promise.promisifyAll(require("fs"));

..... .....

    return fs.readFileAsync(filePath, 'utf8')
        .then(pars.getEx.bind(null, 'user'))
        .then(processExe.bind(null, 'exec'))
        .then(function (result) {
            return result.stdout;
        }, function (error) {
            return error;
        });

The processExe looks like following processExe如下所示

function processExe(method, cmd) {
    return new Promise(function (resolve, reject) {

        var child = child_process[method](cmd, function (error, stdout, stderr) {

The port can be achieved with the following OS 该端口可以通过以下操作系统来实现

https://github.com/baalexander/node-portscanner https://github.com/baalexander/node-portscanner

portscanner.findAPortNotInUse(3000, 4000, 'localhost', function (error, 

port) {

UPDATE 更新

to the processExe I should provide the port like following 到processExe我应该提供以下端口

the port should come from the findPort 该端口应来自findPort

       var options = {
            PORT: port,
        };

and here i also pass options 在这里我也通过选项

var child = child_process[method](cmd,options, function (error, stdout,

If you want/need a parameter, just add a parameter: 如果需要/需要一个参数,只需添加一个参数:

function processExe(method, cmd, port) {
    return new Promise(function (resolve, reject) {
        var child = child_process[method](cmd, {
            PORT: port,
        }, function (error, stdout, stderr) {
            …
        });
        …
    });
}

As always, the asynchronous function should return a promise, how many parameters it has (one, two, or now three) doesn't matter. 与往常一样,异步函数应该返回一个promise,它具有多少个参数(一个,两个或现在三个)无关紧要。 You should not try to find some special way to pass the values that are needed because the function will be used under certain circumstances, that's a separate concern and should not influence the design of your function. 您不应该尝试找到某种特殊的方法来传递所需的值,因为该函数将在某些情况下使用,这是一个单独的问题,并且不应该影响函数的设计。

Now, how to pass additional parameters when the function is invoked within a promise chain? 现在,当在promise链中调用函数时如何传递附加参数? Well, you seem to know that already, function expressions and .bind are at your service. 好吧,您似乎已经知道,函数表达式和.bind已为您服务。 The problem is rather that the source of your argument values is not a single promise, but rather two asynchronous processes - the one that reads and parses a file and the one that finds a port. 问题在于,参数值的来源不是一个承诺,而是两个异步过程-一个读取和解析文件的过程以及一个找到端口的过程。 Since both are async, we expect them both to return promises; 由于两者都是异步的,因此我们希望它们都能返回承诺。 you should be able to promisify portscanner just like you did fs . 你应该能够promisify portscanner ,就像你做了fs

When you have two or more promises, you can combine them using Promise.all : 当您有两个或多个Promise.all ,可以使用Promise.all组合它们:

return Promise.all([
    fs.readFileAsync(filePath, 'utf8').then(pars.getEx.bind(null, 'user')), 
    portscanner.findAPortNotInUseAsync(3000, 4000, 'localhost')
]).then(function(args) {
    return processExe('exec', args[0], args[1]);
})

We can simplify this a bit though by using Promise.join : 我们可以通过使用Promise.joinPromise.join简化Promise.join

return Promise.join(
  fs.readFileAsync(filePath, 'utf8').then(pars.getEx.bind(null, 'user')), 
  portscanner.findAPortNotInUseAsync(3000, 4000, 'localhost'),
  processExe.bind(null, 'exec')
)

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

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