callback not optional #145

Closed
banacorn opened this Issue Jul 4, 2012 · 8 comments

Comments

Projects
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?

Hello,

I just stumbled across the same "issue":

async.map([ '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:

async.map([ '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?

Contributor

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! :)

Contributor

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?

Owner

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
d255901

@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:

alizbazar@gmail.com
Domain alizweb.com 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 github-smtp2-ext5.iad.github.net ([192.30.252.196]:40781 helo=github-smtp2a-ext-cp1-prd.iad.github.net)
by web107.webhotelli.fi with esmtps (TLSv1:DHE-RSA-AES256-SHA:256)
(Exim 4.82)
(envelope-from noreply@github.com)
id 1WTaN7-0003ZF-7t
for alizbazar@alizweb.com; Fri, 28 Mar 2014 19:15:13 +0200
Date: Fri, 28 Mar 2014 10:15:11 -0700
From: Caolan McMahon notifications@github.com
Reply-To: caolan/async reply@reply.github.com
To: caolan/async async@noreply.github.com
Message-ID: caolan/async/issue/145/issue_event/106554019@github.com
In-Reply-To: caolan/async/issues/145@github.com
References: caolan/async/issues/145@github.com
Subject: Re: [async] callback not optional (#145)
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5335ae1f41079_5ceb3faee58212b0139411";
charset=UTF-8
Content-Transfer-Encoding: 7bit
Precedence: list
X-GitHub-Recipient: alizbazar
List-ID: caolan/async <async.caolan.github.com>
List-Archive: https://github.com/caolan/async
List-Post: mailto:reply@reply.github.com
List-Unsubscribe: mailto:unsub+i-5430404-30c5955de2e8d1d8770352024318ff6bf6400a66-736771@reply.github.com,
https://github.com/notifications/unsubscribe/736771__eyJzY29wZSI6Ik5ld3NpZXM6TXV0ZSIsImV4cGlyZXMiOjE3MTE2NDYxMTEsImRhdGEiOnsiaWQiOjkyODkwNH19--7958607255183ddf23315fd08cb36c3434d1d716
X-Auto-Response-Suppress: All
X-GitHub-Recipient-Address: alizbazar@alizweb.com

----==_mimepart_5335ae1f41079_5ceb3faee58212b0139411
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit

Closed #145.


Reply to this email directly or view it on GitHub:
#145
----==_mimepart_5335ae1f41079_5ceb3faee58212b0139411
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit

Closed #145.


Reply to this email directly or view it on GitHub.

----==_mimepart_5335ae1f41079_5ceb3faee58212b0139411--

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