承诺加盟变更承诺一切
我读过承诺join被弃用http://bluebirdjs.com/docs/api/promise.join.html
我的问题是
- 我应该如何改变这个代码来承诺所有 ,如果有更好的方式来编写这个代码(代码工作!),但我想获得您的反馈,但代码应该有相同的逻辑如下
- 有一个工具来节点与蓝鸟 js可以跟踪我是否有未完成的承诺链
createProc = function (fPath) { "use strict"; return Promise.join( fs.readFileAsync(fPath, 'utf8') .then(function (content) { return parseFile(content).getWeb(); }), scan.findInUseAsync(12, 152, 'localhost') .then(envOptions.mod.bind(null, process.env)) ).then(function (args) { return inter.Process('run', args[0], args[1]); }).then(function (result) { return result.stdout; }, function (error) { return error; }); };
如果你想切换到Promise.all()
,它只需要一个承诺数组作为参数,不接受callback(你没有使用)。 所以,你所要做的就是包装你传递给数组的两个promise:
createProc = function (fPath) { "use strict"; return Promise.all([ // added here ^ fs.readFileAsync(fPath, 'utf8') .then(function (content) { return parseFile(content).getWeb(); }), scan.findInUseAsync(12, 152, 'localhost') .then(envOptions.mod.bind(null, process.env)) ]).then(function (args) { return inter.Process('run', args[0], args[1]); }).then(function (result) { return result.stdout; }, function (error) { return error; }); };
有一个工具来节点与蓝鸟js可以跟踪我是否有未完成的承诺链
在问题追踪器中仍然有一个正在讨论的拉取请求,build议的API会让您跟踪这些请求。
如果有更好的方法来写这个代码
那么,你写了很多冗余的代码。 另外, join
函数也是最后一个参数。 Node支持一些非常酷的function:
// module itself should be strict, use arrows they're fun const createProc = fPath => Promise.join( fs.readFileAsync(fPath, 'utf8').then(parseFile).call("getWeb") scan.findInUseAsync(12, 152, 'localhost').then(_ => envOptions.mod(process.env)), args => inter.Process('run', args[0], args[1]); ).get("stdout"); // don't do that silly `return error` thing, propagate exceptions