Skip to content

Instantly share code, notes, and snippets.

@azproduction
Created November 2, 2011 07:56
Show Gist options
  • Save azproduction/1333135 to your computer and use it in GitHub Desktop.
Save azproduction/1333135 to your computer and use it in GitHub Desktop.
Callback chain killer [prototype]
var doit = new Do();
function doSmth(cb) {
cb(null, {});
}
function doSmth2(cb) {
doit.result; // Тут все ответы
doit.previousResult; // Тут ответ предыдущей функции
cb(null, {});
}
function handleError(err){
console.log('pewpew', err); return true; /*return true to continue chain*/
}
var resultArray = doit(doSmth)
.doTry()
.then(doSmth, this) // this - context
.then(doSmth)
.doCatch(handleError)
.then(doSmth,doSmth,doSmth)
.then(function(){console.log(doit.result)});
// w/ then and try catch - elegant code ^^
var resultArray = doit(doSmth)(doSmth, this)(doSmth2)(doSmth,doSmth,doSmth)(function(){console.log(doit.result)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment