承诺加入改变承诺所有

Promise join change to promise all

我读到 promise join 已被弃用 http://bluebirdjs.com/docs/api/promise.join.html

我的问题是

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(),它只需要一个 promises 数组作为参数并且不接受回调(无论如何你都不会使用)。所以,你所要做的就是把你传递的两个承诺包装在一个数组中:

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;
        });
};

there is a tool to node js with bluebird which can track if i've unfinished promise chain

the issue tracker 中仍在积极讨论一个拉取请求,提议的 API 会让您跟踪这些请求。

and if there is better way to write this code

好吧,你写了很多冗余代码。此外,join 将一个函数作为其最后一个参数。 Node 支持一些非常酷的功能:

 // 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