Skip to content

Instantly share code, notes, and snippets.

@jbr
Created January 11, 2011 21:27
Show Gist options
  • Save jbr/775181 to your computer and use it in GitHub Desktop.
Save jbr/775181 to your computer and use it in GitHub Desktop.
in response to recent nodejs post
// In response to this idiom
// (code copied from https://groups.google.com/d/topic/nodejs/wzSUdkPICWg/discussion):
mainWindow.menu("File", function(err, file) {
if (err) throw err;
file.openMenu(function(err, menu) {
if (err) throw err;
menu.item("Open", function(err, item) {
if (err) throw err;
item.click(function(err) {
if (err) throw err;
mainWindow.getChild(type('Window'), function(err, dialog) {
if (err) throw err;
//...
});
});
});
});
});
// Here's a little helper function that simplifies that `if (err) throw err;` noise:
var throwErrors = function( fn ) {
return function( err, args ) {
if ( err ) throw err;
return fn.apply( this, Array.prototype.slice.call( arguments, 1 ));
};
}
mainWindow.menu( "File", throwErrors( function( file ) {
file.openMenu( throwErrors( function( menu ) {
menu.item( "Open", throwErrors( function( item ) {
item.click( throwErrors( function() {
mainWindow.getChild( type( 'Window' ), throwErrors( function( dialog ) {
//...
}));
}));
}));
}));
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment