If I use the Fetch API as follows:
fetch(url)
.then(response => response.json())
.then(console.log)
I understand that this would log the result of the previous "then" (the response data), but why does console.log not require any arguments in this case?
Is there any technical reasoning or documentation behind this, and can any other built-in methods be used in this nature?
Thats simple Javascript, in this case the console.log
requires that argument, what happens here is that the then
function takes a callback as its first argument and internally executes that callback with the argument returned by the last then
function. So it means that you are passing the reference (or a copy, im not sure) of console.log
function instead of directly executing this.
In summary, this:
function a (callback) {
var something = 12345;
callback(something);
}
a(console.log);
is the same as
a(function(something) {
console.log(something);
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.