callback not optional #145

banacorn opened this Issue Jul 4, 2012 · 8 comments


None yet
7 participants

banacorn commented Jul 4, 2012

Collection methods such as forEach, map ... requires an callback, which is not optional and cannot be omitted.

Is this intentional, telling people that they wouldn't need async if there's no callback and nothing to be deferred?


I just stumbled across the same "issue":[ 'foo', 'bar' ], fs.unlink);

Errors of course. As the code I'm writing is just a small demo, I'm not really interested in the results, so I wanted to omit the callback.

What about making the callback optional? It's just an if (callback), and avoids ugly code like this:[ 'foo', 'bar' ], fs.unlink, function(){});

Is @banacorn 's assumption right, i. e. is the callback mandatory on purpose?

maybe they want the type to look tidy and consistent, i guess? (although js is not strongly typed)

@caolan Will you accept a patch?


dougwilson commented Feb 15, 2013

forEach does not require a callback, as it defaults to a no-op callback. @flowlo you can simply write that as:

async.forEach([ 'foo', 'bar' ], fs.unlink);

@banacorn which methods specifically should not require a callback? You listed forEach as an example which actually doesn't require a callback, already fitting in your request. map should always require a callback, because if you don't want results, there is no reason to map when you can simply use forEach.

@dougwilson Yep, that's what I went with.

You can even do

['foo', 'bar'].forEach(fs.unlink);

which should be almost the same in this case (just saying).

👎 because requiring a callback for map makes sense to me now, thanks! :)


brianmaissy commented Feb 18, 2013

I think it's always a good idea to make callbacks optional, either by defaulting to a noop or by replacing all instances of callback(); with if(callback) callback(); Even if the user is misusing the function in its intended usage, as long as no bad side effects are going to happen, better to fail quietly than throwing errors, no?


caolan commented Mar 2, 2013

Yep, happy to make callbacks optional in cases where you're just relying on side-effects for some simple script. If someone wants to send a pull request with tests I'll take a look :)

@neojski neojski added a commit to neojski/async that referenced this issue Aug 3, 2013

@neojski neojski (#145) - add map* no callback tests
Currently failing

@neojski neojski added a commit to neojski/async that referenced this issue Aug 3, 2013

@neojski neojski (#145) - make map* callback optional baba179

caolan closed this Mar 28, 2014

This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a temporary error. The following address(es) deferred:
Domain has exceeded the max emails per hour (126/120 (105%)) allowed. Message will be reattempted later

------- This is a copy of the message, including all the headers. ------
Received: from ([]:40781
by with esmtps (TLSv1:DHE-RSA-AES256-SHA:256)
(Exim 4.82)
id 1WTaN7-0003ZF-7t
for; Fri, 28 Mar 2014 19:15:13 +0200
Date: Fri, 28 Mar 2014 10:15:11 -0700
From: Caolan McMahon
Reply-To: caolan/async
To: caolan/async
Message-ID: caolan/async/issue/145/issue_event/
In-Reply-To: caolan/async/issues/
References: caolan/async/issues/
Subject: Re: [async] callback not optional (#145)
Mime-Version: 1.0
Content-Type: multipart/alternative;
Content-Transfer-Encoding: 7bit
Precedence: list
X-GitHub-Recipient: alizbazar
List-ID: caolan/async <>
X-Auto-Response-Suppress: All

Content-Type: text/plain;
Content-Transfer-Encoding: 7bit

Closed #145.

Reply to this email directly or view it on GitHub:
Content-Type: text/html;
Content-Transfer-Encoding: 7bit

Closed #145.

Reply to this email directly or view it on GitHub.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment