Skip to content

Instantly share code, notes, and snippets.

@jed
Created January 30, 2010 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jed/290698 to your computer and use it in GitHub Desktop.
Save jed/290698 to your computer and use it in GitHub Desktop.
brainstorming promise syntax
// a few ideas for unifying async "promises"
// there are three types of callbacks:
function callback( data ){ sys.puts( "called on both success and error" ) }
function success( data ){ sys.puts( "called on success only" ) }
function error( data ){ sys.puts( "called on error only" ) }
// for any async function, offer a single optional callback.
posix.cat( "config.json", callback );
// the callback can be omitted in the style of partial application,
// to allow returning a promise that takes either one callback,
posix.cat( "config.json" )( callback );
// or takes a success callback and error callback. this is nice because the index
// of each callback is analogous to its UNIX exit code:
posix.cat( "config.json" )( success, error );
// to keep code readable, inline functions should be named:
posix.cat( "config.json" )(
function success(){ ... },
function error(){ ... }
);
// the promise itself would be chained to allow multiple callbacks
posix.cat( "config.json" )
( callback )
( callback )
( success, error );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment