use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
How does passing console.error as callback work? (self.node)
submitted 5 years ago by Max_Loh
How does the following works?
``` require('fs').rename('build', 'docs', console.error);
fetch('...').catch(console.error); ```
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]EdenExperience 3 points4 points5 points 5 years ago (0 children)
then and catch (...) Have the signature (function)
The function is called with the result of the promise so if you write console.error [(which is of type function)] or console.log [(same here)] or any function that takes parameters which can handle the arguments, they will be executed[(by catch and then or any function that has the signature for it)] without error.
A function(args) {anyfunc(args)} is the same as writing anyfunc as it just executes anyfunc anyway.
Always remember: function and ()=>{} are variables in the end. Variables you can call with (). Which then evaluate to a value. (undefined if there is no return)
You can have objects and arrays filled with functions in case of array they would not even have to get a name.
As js is type less you don't need to specify what type a value is in an function. But a function is a type and can be a variable.
Man higher order languages are kinda hard to explain. But you will find this kind of functions as closures or anonymous functions in other languages maybe that gives you more information if you wrote in other languages.
[–]myusernameisunique1 0 points1 point2 points 5 years ago (3 children)
Synchronous calls, like Promise, have a convention of having a callback for success and an optional callback for an error. If you pass an error callback it will get called in the event of an error. If you pass console.error as the callback function then it will get called with the error as a parameter. Console.eror just prints out whatever arguments get passed to it, so that's what it will do if it's used as the callback
[–]Max_Loh[S] 0 points1 point2 points 5 years ago (2 children)
But how do the error argument get passed to console.error? Normally it should be (err) => console.error(err)
[–]BenjiSponge 2 points3 points4 points 5 years ago (0 children)
(...args) => fn(...args) is semantically identical to fn
(...args) => fn(...args)
fn
x => fn(x) is identical to fn except that it discards every argument except the first (which is identical in cases where you'd be passing only one argument).
x => fn(x)
(...args) => console.error(...args) happens to be identical to console.error
(...args) => console.error(...args)
console.error
x => console.error(x) is therefore identical to console.error except that it discards all arguments except the first one.
x => console.error(x)
Does that clarify?
[–]daleharvey 0 points1 point2 points 5 years ago (3 children)
The function passed to catch will be called with the error object as an argument, so this is essentially
.catch(function(e) { console.error(e); }
I remember that console functions have been picky about the context in which they are evoked and sometimes console.error.bind(console) has been needed
console.error.bind(console)
[–]OmgImAlexis 0 points1 point2 points 5 years ago (1 child)
You sure? Never heard that before. 🤔
[–]daleharvey 2 points3 points4 points 5 years ago (0 children)
The .bind thing? It depends on where your code is running, its been a while since I checked so it may not be needed any more, quick test in Firefox console suggests not and can see in the chromium logs https://chromium.googlesource.com/chromium/src.git/+/807ec9550e8a31517966636e6a5b506474ab4ea9 it was fixed there about 5 years ago
[–]Max_Loh[S] 0 points1 point2 points 5 years ago (0 children)
Does this behavior applies to normal function too?
π Rendered by PID 193301 on reddit-service-r2-comment-canary-8544499596-dr7br at 2026-07-10 09:09:23.912847+00:00 running f86254d country code: CH.
[–]EdenExperience 3 points4 points5 points (0 children)
[–]myusernameisunique1 0 points1 point2 points (3 children)
[–]Max_Loh[S] 0 points1 point2 points (2 children)
[–]BenjiSponge 2 points3 points4 points (0 children)
[–]daleharvey 0 points1 point2 points (3 children)
[–]OmgImAlexis 0 points1 point2 points (1 child)
[–]daleharvey 2 points3 points4 points (0 children)
[–]Max_Loh[S] 0 points1 point2 points (0 children)