07/09/2018, 16:11
JavaScript Asynchronous: Promise by Q
1. A simple story written in complex way ( by Q ) async_func = -> d = Q.defer() console .log( "M: Should I?" ) # Do something async setTimeout(( -> console .log( "M: OK, give him" ) d.resolve( "M: Here're all mine" ) ), 10000 ) console ...
1. A simple story written in complex way (by Q)
async_func = ->
d = Q.defer()
console.log("M: Should I?")
# Do something async
setTimeout((->
console.log("M: OK, give him")
d.resolve("M: Here're all mine")
), 10000)
console.log("M: Ahhh, wait for my thinking. I promise!")
d.promise
main_func = ->
console.log("T: Free hand! Give me your money!")
console.log("T: You have 10 secs to think...")
async_func().then((async_promise)->
console.log(async_promise)
).done((->
console.log("T: OK, you're dead! BAM")
))
console.log("Hmmm?")
setTimeout((->
console.log("T: My gun is waiting")
), 5000)
main_func()
2. The story
> T: Free hand! Give me your money!
> T: You have 10 secs to think...
> M: Should I?
> M: Ahhh, wait for my thinking. I promise!
> Hmmm?
> T: My gun is waiting
> M: OK, give him
> M: Here's all mine
> T: OK, you're dead! BAM