如何使用 Bluebird 保证 Node 的 child_process.exec 和 child_process.execFile 函数? [英] How to promisify Node's child_process.exec and child_process.execFile functions with Bluebird?

查看:13
本文介绍了如何使用 Bluebird 保证 Node 的 child_process.exec 和 child_process.execFile 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Node.js 下使用 Bluebird promise 库,很棒!但我有一个问题:

I'm using the Bluebird promise library under Node.js, it's great! But I have a question:

如果您查看 Node 的 child_process.execchild_process.execFile 你可以看到这两个函数都返回一个 ChildProcess 对象.

If you take a look at the documentation of Node's child_process.exec and child_process.execFile you can see that both of these functions are returning a ChildProcess object.

那么推荐的承诺这些功能的方法是什么?

So what's the recommended way to promisify such functions?

请注意以下工作(我得到一个 Promise 对象):

Note that the following works (I get a Promise object):

var Promise = require('bluebird');
var execAsync = Promise.promisify(require('child_process').exec);
var execFileAsync = Promise.promisify(require('child_process').execFile);

但是如何才能访问原始 Node.js 函数的原始返回值呢?(在这些情况下,我需要能够访问最初返回的 ChildProcess 对象.)

But how can one get access to the original return value of the original Node.js functions? (In these cases I would need to be able to access the originally returned ChildProcess objects.)

任何建议将不胜感激!

这是使用 child_process.exec 函数的返回值的示例代码:

Here is an example code which is using the return value of the child_process.exec function:

var exec = require('child_process').exec;
var child = exec('node ./commands/server.js');
child.stdout.on('data', function(data) {
    console.log('stdout: ' + data);
});
child.stderr.on('data', function(data) {
    console.log('stderr: ' + data);
});
child.on('close', function(code) {
    console.log('closing code: ' + code);
});

但是如果我使用 exec 函数的promisified版本(上面的 execAsync ),那么返回值将是一个承诺,而不是一个 ChildProcess 对象.这才是我要说的真正问题.

But if I would use the promisified version of the exec function ( execAsync from above ) then the return value will be a promise, not a ChildProcess object. This is the real problem I am talking about.

推荐答案

听起来您想从调用中返回两件事:

It sounds like you'd like to return two things from the call:

  • 子进程
  • 在 ChildProcess 完成时解决的承诺

那么承诺这些功能的推荐方式"?不要.

So "the recommended way to promisify such functions"? Don't.

你不在公约范围内.Promise 返回函数应该返回一个 Promise,仅此而已.您可以返回一个包含两个成员的对象(ChildProcess 和 promise),但这只会让人们感到困惑.

You're outside the convention. Promise returning functions are expected to return a promise, and that's it. You could return an object with two members (the ChildProcess & the promise), but that'll just confuse people.

我建议调用 unpromisified 函数,并根据返回的 childProcess 创建一个 promise.(也许把它包装成一个辅助函数)

I'd suggest calling the unpromisified function, and creating a promise based off the returned childProcess. (Maybe wrap that into a helper function)

这样,对于下一个阅读代码的人来说就非常明确了.

This way, it's quite explicit for the next person who reads the code.

类似于:

var Promise = require('bluebird');
var exec = require('child_process').execFile;

function promiseFromChildProcess(child) {
    return new Promise(function (resolve, reject) {
        child.addListener("error", reject);
        child.addListener("exit", resolve);
    });
}

var child = exec('ls');

promiseFromChildProcess(child).then(function (result) {
    console.log('promise complete: ' + result);
}, function (err) {
    console.log('promise rejected: ' + err);
});

child.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});
child.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});
child.on('close', function (code) {
    console.log('closing code: ' + code);
});

这篇关于如何使用 Bluebird 保证 Node 的 child_process.exec 和 child_process.execFile 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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