Last active
August 29, 2015 14:10
-
-
Save metasansana/1dacf5f90a765250a3b3 to your computer and use it in GitHub Desktop.
Broken by promises
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
/** | |
I've been using promises a lot lately after my first dance in callback hell. | |
I have not read (but did skim) any of the specs nor do I intend too. I also find the bedtime stories about | |
promises cute but far to wordy when I'm in a hurry. | |
So I decided to type up this for future reference, when next time I chain some promises together and am completly confused as | |
to why they don't work the way I expect. | |
Think of promises like an asynchronous try, catch, finally | |
**/ | |
//That is to say: | |
javascript | |
loadWebPage(). | |
then(null, onLoadWebPageError). | |
then(saveToDatabase). | |
then(null, onSaveToDatabaseError). | |
then(emailResults). | |
then(null, onEmailSaveError). | |
done(cleanup); | |
//if almost like saying: | |
try { | |
loadWebPage(); | |
saveToDatabase(); | |
emailResults(); | |
catch(LoadWebPageException e) { | |
onLoadWebPageError(); | |
}catch(SaveToDatabaseException e) { | |
onSaveToDatabaseError(); | |
}catch(EmailResultsException e) { | |
onEmailSaveError(); | |
}finally{ | |
cleanup(); | |
} | |
//Of course the above could be done in node except that almost all io is async so it won't quite work, that's why | |
//we use promises. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment