I have some squares sliding down the page, in a chained promise fashion: https://jsfiddle.net/u4x0qwfo/3
The code is:
new Promise(function(resolve, reject) {
$("#shape").css({
top: 100
});
setTimeout(function() {
resolve();
}, 1000);
}).then(function() {
return new Promise(function(resolve, reject) {
$("#shape2").css({
top: 100
});
setTimeout(function() {
resolve();
}, 1000);
});
}).then(function() {
return new Promise(function(resolve, reject) {
$("#shape3").css({
top: 100
});
setTimeout(function() {
resolve();
}, 1000);
});
}).then(function() {
return new Promise(function(resolve, reject) {
$("#shape4").css({
top: 100
});
setTimeout(function() {
resolve();
}, 1000);
});
});
(the code doesn't run as well inside of a snippet here: here the first square has already slide down initially).
So to see what promise is returned by the fulfillment handler, and what promise is returned by .then()
, I have https://jsfiddle.net/u4x0qwfo/10/
The code is:
let foobar;
let lala = new Promise(function(resolve, reject) {
$("#shape").css({
// ...
}).then(function() {
foobar = new Promise(function(resolve, reject) {
// ...
return foobar;
});
lala.then(function() {
console.log("checking:", lala, foobar, lala === foobar);
return new Promise(function(resolve, reject) {
In the debug console, we can see that the promises are different. But why do they have to be different?
Actually inside the docs of .then()
return value, it is said that:
[if
.then()
] returns another pending promise object, the resolution/rejection of the promise returned bythen
will be subsequent to the resolution/rejection of the promise returned by the handler. Also, the value of the promise returned bythen
will be the same as the value of the promise returned by the handler.
It suggests that the two promises are different (the one returned by the fulfillment handler and the one returned by .then()
). (I can't find the description in the ES6 specs). The question is why? Can't they be the same promise?
The second part says:
Also, the value of the promise returned by then will be the same as the value of the promise returned by the handler.
I thought initially it meant "the promise returned by then will be the same the promise returned by the handler", but only to find out it actually meant: "the resolved value of the promise returned by then
will be the same as the resolved value of the promise returned by the handler". (is this the proper way to describe it?).
Inside of Google Chrome, both promises will show the same resolved value of 123456
: https://jsfiddle.net/u4x0qwfo/11/