Skip to content

Instantly share code, notes, and snippets.

@blackbing
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save blackbing/a9dad5c141f51b65b650 to your computer and use it in GitHub Desktop.
Save blackbing/a9dad5c141f51b65b650 to your computer and use it in GitHub Desktop.
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