Skip to content

Instantly share code, notes, and snippets.

@Gozala
Last active December 10, 2015 15:58
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 Gozala/4457303 to your computer and use it in GitHub Desktop.
Save Gozala/4457303 to your computer and use it in GitHub Desktop.
Escape callback hell with macros! It takes only 15 lines of wisp to solve this problem forever!! Excited ? Checkout https://github.com/Gozala/wisp
(defmacro =>
[& operations]
`(fn [callback]
~(reduce-list
(reverse operations)
(fn [form operation]
(cons
(first operation)
(concat-list
(cons 'result (rest operation))
`((fn [error result]
(if error
(callback error)
~form))))))
'(callback error result))))
(=>
(read-file "passwords.txt")
(connect-to db)
(run-query "select * from bar")
(write content))
undefined;
function(callback) {
return readFile(result, "passwords.txt", function(error, result) {
return error ?
callback(error) :
connectTo(result, db, function(error, result) {
return error ?
callback(error) :
runQuery(result, "select * from bar", function(error, result) {
return error ?
callback(error) :
write(result, content, function(error, result) {
return error ?
callback(error) :
callback(error, result);
});
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment