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
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
inimino  
View profile  
 More options Feb 1 2010, 6:22 pm
From: inimino <inim...@inimino.org>
Date: Mon, 01 Feb 2010 16:22:07 -0700
Local: Mon, Feb 1 2010 6:22 pm
Subject: Re: [nodejs] Re: Promises, callbacks, Continuables and more
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/

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.