Writing a demo script to understand promises I nested multiple promises (using promises.all() to proceed after all promises are resolved) inside a then(). The then()s of the nested promises do not get resolved:
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:
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?
Writing a demo script to understand promises I nested multiple promises (using promises.all() to proceed after all promises are resolved) inside a then(). The then()s of the nested promises do not get resolved:
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:
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?
Share Improve this question asked Jan 10, 2016 at 23:28 r0bsr0bs 3173 silver badges9 bronze badges 10- 1 What is it you're actually trying to do here? – Jared Smith Commented Jan 10, 2016 at 23:34
-
Async functions inside of a
.then()
handler that don't themselves return a promise from the.then()
handler are pletely disconnected from the promise chain entirely and do not affect it at all. There appear to be lots of things wrong here, but I don't know what you're really trying to achieve so I'm not sure exactly what to suggest. – jfriend00 Commented Jan 10, 2016 at 23:36 -
In addition, you don't do
new Promise.all(array)
, you just doreturn Promise.all(array)
. – jfriend00 Commented Jan 10, 2016 at 23:37 -
Inside of
returnapromise()
, you have multiple async operations that are disconnected from one another, all running in parallel, with only one that actually calls the resolve function of the parent promise. So many things wrong, not sure where to start without knowing the actual objective. – jfriend00 Commented Jan 10, 2016 at 23:40 - @jfriend00 that's why I asked :) – Jared Smith Commented Jan 10, 2016 at 23:41
1 Answer
Reset to default 5In your first version of returnapromise()
, you create two promises, one nested inside of the other.
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. 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.
You can rewrite returnapromise()
like this:
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/jfriend00/k1q60Lep/