Javascript 从Q Promise运行并获得连续的进度

Javascript 从Q Promise运行并获得连续的进度,javascript,node.js,promise,q,Javascript,Node.js,Promise,Q,我有这段代码,我希望这两个承诺按顺序运行,结果如下: a progress: 20 a progress: 40 a progress: 60 a progress: 80 a progress: 100 a resolve: a b progress: 20 b progress: 40 b progress: 60 b progress: 80 b progress: 100 b resolve: b 但我明白了: a progress: 20 b progress: undefined

我有这段代码,我希望这两个承诺按顺序运行,结果如下:

a progress: 20
a progress: 40
a progress: 60
a progress: 80
a progress: 100
a resolve: a

b progress: 20
b progress: 40
b progress: 60
b progress: 80
b progress: 100
b resolve: b
但我明白了:

a progress: 20
b progress: undefined
a progress: 40
b progress: undefined
a progress: 60
b progress: undefined
a progress: 80
b progress: undefined
a progress: 100
a resolve: a
b progress: undefined
b progress: 20
b progress: 40
b progress: 60
b progress: 80
b progress: 100
b resolve: b
这是我的全部代码:

var a =  function(){
        return q.Promise(function(resolve, reject, notify){
            var percentage = 0;
            var interval = setInterval(function() {
                percentage += 20;
                notify(percentage);
                if (percentage === 100) {
                    resolve("a");
                    clearInterval(interval);
                }
            }, 500);
        });
    };

    var b =  function(){
        return q.Promise(function(resolve, reject, notify){
            var percentage = 0;
            var interval = setInterval(function() {
                percentage += 20;
                notify(percentage);
                if (percentage === 100) {
                    resolve("b");
                    clearInterval(interval);
                }
            }, 500);
        });
    };


    a().then(function(res) {
            console.log('a resolve: '+res);
            return b();
        },
        errorHandler,
        function(progress){
            console.log('a progress: '+progress);
        })

        //call b()
        .then(function(res){
            console.log('b resolve: '+res);
        },
        errorHandler,
        function(progress){
            console.log('b progress: '+progress);
        });

    function errorHandler(err) {
        console.log('Error Handler:', err);
    }
你可以用

使用
fin
也可以:

a()
.then(
    function(res) {
        console.log('a resolve: '+res);
    },
    errorHandler,
    function(progress){
        console.log('a progress: '+progress);
    })
.fin(function() {
    b().then(function(res) {
        console.log('b resolve: '+res);
    },
    errorHandler,
    function(progress){
        console.log('b progress: '+progress);
    });
});
让我们看看原始代码的流程:

a().then(function(res) {
        console.log('a resolve: '+res);
        return b(); // (0) here we only create promise for 'b'
    },
    errorHandler,
    function(progress){
        console.log('a progress: '+progress); // (1) here Q calls progress's callback
    })

    //call b()
    .then(function(res){
        console.log('b resolve: '+res); // (2) it's called in the end
    },
    errorHandler,
    function(progress){
        console.log('b progress: '+progress);  // (1) and here Q calls progress's callback
    });

“b progress:undefined”是因为完全没有进度,但调用了回调。在承诺未兑现时调用进度回调。当您返回某个内容或调用“resolve”时,承诺将被完全填满。

我使用的方式不是链接?根据文件,它是!您只是因为返回b(),将所有代码嵌套在a的then节中。但是为什么呢?我不使用嵌套。它就像一个文档所说的“您可以在处理程序内部或外部链接承诺”。我想在外面用一个简单的代码。它说这两者是等价的,但为什么这不起作用呢?您的代码在处理程序内部,我的代码在处理程序外部,但您的工作,而不是我的。我正在考虑解释。实际上,我的代码是解决部分的工作,我的意思是解决的顺序,但只有进度部分像这样运行parallel@Fcoder:不推荐使用进度通知,因为它不遵循这些语义。如果链接,事件将在所有处理程序中冒泡,因此如果要单独处理它们,则需要嵌套。
a().then(function(res) {
        console.log('a resolve: '+res);
        return b(); // (0) here we only create promise for 'b'
    },
    errorHandler,
    function(progress){
        console.log('a progress: '+progress); // (1) here Q calls progress's callback
    })

    //call b()
    .then(function(res){
        console.log('b resolve: '+res); // (2) it's called in the end
    },
    errorHandler,
    function(progress){
        console.log('b progress: '+progress);  // (1) and here Q calls progress's callback
    });