Last active
April 5, 2023 02:12
-
-
Save pketh/2d036345483b690d5da020f2903891d7 to your computer and use it in GitHub Desktop.
Promises in Coffeescript
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
# Create a promise: | |
myCoolPromise = new Promise (resolve, reject) -> | |
# do a thing | |
success = true | |
if success | |
resolve 'stuff worked' | |
else | |
reject Error 'it broke' | |
# Use the promise: | |
myCoolPromise.then (result) -> | |
console.log result # returns 'stuff worked' | |
.catch (error) -> | |
console.log error # returns error: 'it broke' | |
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
# You can also define a promise as a function: | |
myCoolPromise = () -> | |
return new Promise (resolve, reject) -> | |
# do a thing | |
success = true | |
if success | |
resolve 'stuff worked' | |
else | |
reject Error 'it broke' | |
# Use the promise: | |
myCoolPromise(param).then (response) -> | |
console.log 'success', response | |
.catch (error) -> | |
console.error 'failed', error | |
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
# You can also chain thens in a promise: | |
## (handy for running a bunch of async things in sequence) | |
myCoolPromise.then (result) -> | |
console.log result # returns 'stuff worked' | |
return 'abc' | |
.then (response) -> | |
console.log response # returns 'abc' | |
.catch (error) -> | |
console.error error | |
asyncRecovery() # asyncRecovery error handler can have it's own promise chain | |
.then () | |
console.log 'all done' | |
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
Promise.all [myCoolPromise, myOtherCoolPromise].then (arrayOfResults)-> | |
# this part will run after all promises have finished | |
console.log 'yay my promises finished' |
there are fancier promise things you can do (see http://www.html5rocks.com/en/tutorials/es6/promises/) but the above will get you through 95% of life in a non scary way.
Thanks, it helps me 👍
appreciate the examples
for 2-promises-as-function.coffee should line 3 be this instead?: myCoolPromise = (param) ->
It's not clear to me how the param is used
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
native promises work in node and all modern browsers (not IE11, but whatever)