Created by Fabien Garcia
a declaration that something will or will not be done, given, etc., by one
dictionary.com
ken = new PerfectMan()
barbie = new PerfectWoman()
So this is the classic process
ken.marry(barbie)
ken.marry(barbie)
.then => ken.find(job)
.then => child = Sex(ken, barbie)
.then (child) => ken.takeCare(child); barbie.takeCare(child)
.then => ken.die()
.then => barbie.die()
ted = new Man()
robin = new Woman()
Ted want to marry but Robin not really ...
ted.marry(robin)
.then => ted.find("job")
.fail => robin = ted.find("otherWoman")
.then => child = Sex(ken, robin)
.then (child) => ken.takeCare(child); robin.takeCare(child)
.then => ken.die()
.then => barbie.die()
.fail => HIMYM = Factory("serie")
me = new Man()
barbie = new PerfectWoman()
$.when(me.find("job"), me.do("sport"),
me.earn("money"), me.do("surgery"))
.then => me.marry(barbie)
.then => child = Sex(me, barbie)
.then(child) => me.takeCare(child)
.then => me.die()
.then => barbie.die()
.fail(error) => me.find("otherWoman")
Code is just a story
user.findWhere(name:"john").success(user) ->
Email.send(user.email).success(response) ->
fs.write response, (status) ->
user.save(status: "message send"). success(err, res) ->
console.log "workflow finished"
user.findWhere(name:"john")
.then (user) -> Email.send(user.email)
.then (response) -> fs.write response
.then (status) -> user.save(status: "message send")
.then (status) -> console.log "Workflow finished"
checkAllFetched = (collection) =>
collection.fetched = true
if @collection1.fetched and @collection2.fetched
@prepareData()
fetchOptions =
success: checkAllFetched
data: @params
@collection1.fetch fetchOptions
@collection2.fetch fetchOptions
$.when(@collection1.fetch(data: @params),
@collection2.fetch(data: @params))
.done => @prepareData()
find: (email, callback) ->
@query "SELECT `id`, `email` WHERE email = `?` LIMIT 1", email, (err, res) ->
callback err, res[0]
find: (email) ->
deferred = Q.defer()
@query "SELECT `id`, `email` WHERE email = `?` LIMIT 1",
email, (err, res) ->
if err
deferred.reject new Error(err)
else
deferred.resolve res
return deferred.promise
find (email) ->
Q.nfcall @query, "SELECT `id`, `email` WHERE email = `?` LIMIT 1", email
or
find (email) ->
Q.ninvoke @, "query", "SELECT `id`, `email` WHERE email = `?` LIMIT 1", email
promiseFunc = ->
deferred = new jQuery.Deferred()
ayncFunction param, (err, res) ->
if err
deferred.reject err
else
deferred.resolve res
return deferred.promise()
It will be used
$.when(promiseFunc).then ((res) ->
alert status + ", things are going well"
$.when($.get("http://url.com"))
.then (data) ->
alert data + ", things are going well"
.fail (err) ->
console.log err