简体   繁体   English

node.js promise:嵌套在then()链中的promise的then()不会被解析

[英]node.js promise: then() of promise nested inside a then() chain doesn't get resolved

Writing a demo script to understand promises I nested multiple promises (using promises.all() to proceed after all promises are resolved) inside a then(). 编写一个演示脚本来理解promises我嵌套了多个promises(使用promises.all()在then()之后继续解决所有promises之后)。 The then()s of the nested promises do not get resolved: 嵌套的promises的then()s无法解析:

var Promise = require("bluebird");

var array = [];

// push promises onto array
new Promise(function(resolve, reject) {
    setTimeout(function() { 
        for (var i = 5 - 1; i >= 0; i--) {
            array.push(returnapromise());
            console.log("pushed promise number", i, "onto array");
        }
        resolve();
    }, 300);
})

.then(function() {
    new Promise.all(array).then(function() {
        console.log("\nall done\n");
    });
});

// function that returns a promise
var returnapromise = function() {
    return new Promise(function(yolo) {

        new Promise(function() {
            setTimeout(function() { 
                console.log("async function within nested promise");
            }, 200);
        })

        .then(function() {
            setTimeout(function() { 
                console.log("async function within then one")
            }, 100);
        })

        .then(function() {
            setTimeout(function() { 
                console.log("async function within then two")
                yolo();
            }, 50);
        });

    }) // eof returned promise

}; // eof returnapromise()

However the desired goal can be achieved using callbacks inside the nested promise, like this: 但是,使用嵌套promise中的回调可以实现所需的目标,如下所示:

var Promise = require("bluebird");

var array = [];

// push promises onto array
new Promise(function(resolve, reject) {
    setTimeout(function() { 
        for (var i = 5 - 1; i >= 0; i--) {
            array.push(returnapromise());
            console.log("pushed promise number", i, "onto array");
        }
        resolve();
    }, 300);
})

.then(function() {
    new Promise.all(array).then(function() {
        console.log("\nall done\n");
    });
});

// function that returns a promise
var returnapromise = function() {
    return new Promise(function(yolo) {

        new Promise(function() {
            setTimeout(function() { 
                console.log("async function within nested promise");
                one()
            }, 200);

            var one = function() {
                setTimeout(function() { 
                    console.log("cb one")
                    two();
                }, 100);
            };

            var two = function() {
                setTimeout(function() { 
                    console.log("cb two")
                    yolo();
                }, 50);
            };

        }) // eof nested promise

    }) // eof returned promise

}; // eof returnapromise()

How can I write nested promises which's then()s actually get resolved? 我如何编写嵌套的promises,然后()s实际得到解决?

In your first version of returnapromise() , you create two promises, one nested inside of the other. 在你的第一个版本的returnapromise() ,你创建了两个promises,一个嵌套在另一个中。

The inside promise is never resolved (there is no code to ever resolve it). 内部承诺永远不会被解决(没有代码可以解决它)。 As such, it never calls its .then() handlers which means that the yolo() function is never called which means the outer promise is never resolved. 因此,它永远不会调用它的yolo() .then()处理程序,这意味着永远不会调用yolo()函数,这意味着永远不会解析外部promise。 So, it is just stuck forever. 所以,它只是永远停滞不前。

You can fix that by resolving the inner promise in that first setTimeout() , then then that still leaves the second setTimeout() disconnected from the whole promise chain. 您可以通过在第一个setTimeout()解析内部promise来解决这个问题,然后这仍然会使第二个setTimeout()与整个promise链断开连接。

You can rewrite returnapromise() like this: 您可以像这样重写returnapromise()

function delay(t, val) {
    return new Promise(function(resolve) {
        setTimeout(function() {
            console.log(val);
            resolve(val);
        }, t);
    });
}

// function that returns a promise
var returnapromise = function() {
    return delay(200, "async function within nested promise").then(function() {
        return delay(100, "async function within then one");
    }).then(function() {
        return delay(50, "async function within then two");
    });
}; // eof returnapromise()

Working demo: https://jsfiddle.net/jfriend00/k1q60Lep/ 工作演示: https//jsfiddle.net/jfriend00/k1q60Lep/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM