Node.js promise: then () the promise nested inside the then () chain will not be resolved

Writing a demo script to understand promises I have enclosed several promises (using promises.all () to continue after all promises are resolved) inside a then (). Then () s nested promises are not allowed:

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 a nested promise, for example:

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 that then () s will really be resolved?

+4
source share
1 answer

In your first version, returnapromise()you create two promises, one nested inside the other.

( , ). , .then(), , yolo() , , . , .

, setTimeout(), setTimeout() .

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()

: https://jsfiddle.net/jfriend00/k1q60Lep/

+6

Source: https://habr.com/ru/post/1623685/


All Articles