问题描述
我正在使用Node.js下的Bluebird promise库,这太棒了!但我有一个问题:
I'm using the Bluebird promise library under Node.js, it's great! But I have a question:
如果你看一下Node的文档和,你可以看到这两个函数都在返回一个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.)
任何建议都将不胜感激!
Any suggestion would be appreciated!
编辑:
这是一个使用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
- 在ChildProcess完成时解析的承诺
那么推荐的方式来宣传这些功能? 不要。
So "the recommended way to promisify such functions"? Don't.
你不在大会之外。承诺返回函数有望返回一个承诺,就是这样。你可以返回一个有两个成员的对象(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函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!