Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
query = (task, callback)->
#do something
callback(task + ' finished')
#nested way
query('task1', (msg)->
console.log msg
query('task2', (msg)->
console.log msg
query('task3', (msg)->
console.log msg
)
)
)
#function call function
task1 = ->
query('task1', (msg)->
console.log 'function ' + msg
task2()
)
task2 = ->
query('task2', (msg)->
console.log 'function ' + msg
task3()
)
task3 = ->
query('task3', (msg)->
console.log 'function ' + msg
)
task1()
#Deferred
query_dfr = (task)->
_dfr = $.Deferred()
query(task, (msg)->
_dfr.resolve(msg)
)
_dfr.promise()
query_dfr('task1')
.pipe((msg)->
console.log ('Deferred ' + msg )
query_dfr('task2')
).pipe((msg)->
console.log ('Deferred ' + msg )
query_dfr('task3')
).pipe( (msg)->
console.log ('Deferred ' + msg )
)
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment