Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Promises, callbacks, Continuables and more

X-BeenThere: nodejs@googlegroups.com
Received: by 10.231.24.74 with SMTP id u10ls1312199ibb.2.p; Mon, 01 Feb 2010 
	15:22:38 -0800 (PST)
Received: by 10.231.155.18 with SMTP id q18mr531917ibw.12.1265066558548;
        Mon, 01 Feb 2010 15:22:38 -0800 (PST)
Received: by 10.231.155.18 with SMTP id q18mr531916ibw.12.1265066558528;
        Mon, 01 Feb 2010 15:22:38 -0800 (PST)
Return-Path: <inim...@inimino.org>
Received: from atekomi.inimino.org (atekomi.inimino.org [67.207.138.202])
        by gmr-mx.google.com with ESMTP id 24si673223iwn.11.2010.02.01.15.22.37;
        Mon, 01 Feb 2010 15:22:37 -0800 (PST)
Received-SPF: pass (google.com: best guess record for domain of inim...@inimino.org designates 67.207.138.202 as permitted sender) client-ip=67.207.138.202;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: best guess record for domain of inim...@inimino.org designates 67.207.138.202 as permitted sender) smtp.mail=inim...@inimino.org
Received: from c-76-25-188-35.hsd1.co.comcast.net ([76.25.188.35] helo=[192.168.1.143])
	by atekomi.inimino.org with esmtpa (Exim 4.63)
	(envelope-from <inim...@inimino.org>)
	id 1Nc5bI-00040d-2D; Mon, 01 Feb 2010 23:22:36 +0000
Message-ID: <4B67621F.8000305@inimino.org>
Date: Mon, 01 Feb 2010 16:22:07 -0700
From: inimino <inim...@inimino.org>
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.5) Gecko/20100108 Icedove/3.0
MIME-Version: 1.0
To: nodejs@googlegroups.com
Subject: Re: [nodejs] Re: Promises, callbacks, Continuables and more
References: <4B64AD2E.30709@inimino.org>	 <DD27188C-649D-4C3F-86B6-5F7651F498C0@creationix.com>	 <f67206b91001301505t6316d04ci6dce21ed2c87b2b4@mail.gmail.com>	 <4B659B88.3080407@inimino.org>	 <21ee31951001311123u7c16c403hb84d9fe780916c49@mail.gmail.com>	 <21ee31951001311144y761e53caqf2a1f3377c67481@mail.gmail.com>	 <32ac9e0d-da7b-41bd-8bfa-ea5094d08684@y7g2000prc.googlegroups.com>	 <20bb22ec-b431-420d-9a05-70d365fda318@v25g2000yqk.googlegroups.com> <6de454e91002011351u1b02a509wb673a99cb09ef2ea@mail.gmail.com>
In-Reply-To: <6de454e91002011351u1b02a509wb673a99cb09ef...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

On 2010-02-01 14:51, Jan Sch�tze wrote:
> 2010/2/1 Felix Geisend�rfer <fe...@debuggable.com>:
>> * Having a single callback for both error/success
> +1
> 
> This has the advantage that you don't have to write the logic for
> chained callbacks two times and it's not up to the library developer
> to decide what an "error"/"success" is.

One issue with most of the suggestions I've seen for this 
so far is that they require type testing to distinguish 
success or failure.

If you're going to have a single callback, make it 
unambiguous, so that the same pattern can be used regardless 
of the range of values that might be returned.

In Felix's[1] example:

function loadConfig() {
  return posix.cat('config.json')
    (function(config, error) {
      return config || posix.cat('configuration.json')
    })
    (function(config, error) {
      return error || JSON.parse(config);
    })
}

[1]: https://gist.github.com/85c313e588a3fc9d8636

This works only until you need to return undefined, (or 0, 
"", null, or Boolean false...) as a successful asynchronous 
result.

I like to use values which are either [0,<error>] or [1,<data>], 
which gives[2]:

function loadConfig() {
  return posix.cat('config.json')
    (function(either) {
      return either[0] ? either[1] : posix.cat('configuration.json')
    })
    (function(either) {
      return either[0] ? JSON.parse(either[1]) : "Could not read conf file: "+either[1]
    })
}

[2]: https://gist.github.com/67cbf71c79fd5b6bf3a1

Or if you prefer separate arguments, make the first indicate 
success and the second be either the successful result or the 
error:

function loadConfig() {
  return posix.cat('config.json')
    (function(succeeded,value) {
      return succeeded ? value : posix.cat('configuration.json')
    })
    (function(succeeded,value) {
      return succeeded ? JSON.parse(value) : "Could not read conf file: "+value
    })
}

-- 
http://inimino.org/~inimino/blog/