Skip to content

Instantly share code, notes, and snippets.

@sillypog
Created May 28, 2015 22:08
Show Gist options
  • Save sillypog/8e3583f4679ecd9d50c9 to your computer and use it in GitHub Desktop.
Save sillypog/8e3583f4679ecd9d50c9 to your computer and use it in GitHub Desktop.
Q Promises
Q = require 'q'
init = () ->
console.log 'init'
promises = [checkS3FolderExists('MyData1'), checkS3FolderExists('MyData2')]
Q.all(promises).then (results)->
console.log 'Done!'
console.log results
console.log 'init complete'
checkS3FolderExists = (folder) ->
console.log "checking for folder #{folder}"
params =
folder: folder
s3Request('listObjects', params)
s3Request = (task, params) ->
deferred = Q.defer()
setTimeout ->
deferred.resolve("Found folder: #{params.folder}")
, 1000
deferred.promise
init()
###
Generated output:
init
checking for folder MyData1
checking for folder MyData2
init complete
Done!
[ 'Found folder: MyData1', 'Found folder: MyData2' ]
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment