Created
January 30, 2010 19:59
-
-
Save jed/290698 to your computer and use it in GitHub Desktop.
brainstorming promise syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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