Last active
December 10, 2015 15:58
-
-
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
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
(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)) |
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
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