admin管理员组文章数量:1322666
I'm learning about JavaScript Fetch API and I'm a bit confusing about Promises.
Consider this dummy example that prints "ok" in the console:
fetch(".")
.then(function(response) { // first then() call
return response;
}).then(function(response) { // second then() call
console.log("ok");
});
The page about the Response Object of the Fetch API says:
The fetch() call returns a promise, which resolves with the Response object associated with the resource fetch operation.
Well, since fetch()
returns a Promise object I can understand that the first then()
call works fine because a Promise object has this method. But the Response object returned in the chained call is not a Promise object. However the second call to then()
method works!
Altering the dummy example prints undefined
in the first console.log()
:
fetch(".")
.then(function(response) { // first then() call
console.log(response.then)
return response;
}).then(function(response) { // second then() call
console.log("ok");
});
My questions is: Why this works? How the second call to then()
works since the returned object does not have this method? Is it a kind of syntax sugar?
Thanks!
I'm learning about JavaScript Fetch API and I'm a bit confusing about Promises.
Consider this dummy example that prints "ok" in the console:
fetch(".")
.then(function(response) { // first then() call
return response;
}).then(function(response) { // second then() call
console.log("ok");
});
The page about the Response Object of the Fetch API says:
The fetch() call returns a promise, which resolves with the Response object associated with the resource fetch operation.
Well, since fetch()
returns a Promise object I can understand that the first then()
call works fine because a Promise object has this method. But the Response object returned in the chained call is not a Promise object. However the second call to then()
method works!
Altering the dummy example prints undefined
in the first console.log()
:
fetch(".")
.then(function(response) { // first then() call
console.log(response.then)
return response;
}).then(function(response) { // second then() call
console.log("ok");
});
My questions is: Why this works? How the second call to then()
works since the returned object does not have this method? Is it a kind of syntax sugar?
Thanks!
Share Improve this question edited May 18, 2017 at 13:39 Bruno Peres asked May 17, 2017 at 2:40 Bruno PeresBruno Peres 16.4k6 gold badges56 silver badges92 bronze badges 2-
3
"How the second call to then() works since the returned object does not have this method?" - The
.then()
function calls themselves occur immediately and each return a promise object, they're not dependent on the previous callback in the chain returning something with a.then()
method. – nnnnnn Commented May 17, 2017 at 2:45 - To support @nnnnnn, here: stackoverflow./a/27716518/4279440 is a good explanation of this behavior. – Travis Clarke Commented May 17, 2017 at 2:49
1 Answer
Reset to default 11But the
Response
object returned in the chained call is not a Promise object. However the second call tothen()
method works!
Yes, because the second .then()
invocation is on the return value of the first then
call, not on the response. The promise then
method always returns a promise - which makes it chainable. It does not return the return value of the asynchronous callback - for that it would need to peek into the future.
Look closely:
const promise1 = fetch(".");
const promise2 = promise1.then(function(response) { // first then() call
return response;
});
const promise3 = promise2.then(function(response) { // second then() call
console.log("ok");
});
It's not
fetch(".").then(function(response) { // outer then() call
return response.then(function() { // inner then() call
console.log("ok");
});
});
which indeed doesn't work without response
being a promise.
本文标签: JavaScript Promises chaining promises with nonpromise objects Why it worksStack Overflow
版权声明:本文标题:JavaScript Promises: chaining promises with non-promise objects. Why it works? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742117047a2421515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论