async-waiter

0.1.1 • Public • Published

async-waiter

Higher-level functions to simplify waiting for multiple concurrent async operations to finish.

Install

npm install --save async-waiter

Example

 
waiter = require 'waiter'
 
# Wait for `doFoo()` and `doBar()` to finish, then call `cb` 
doFooAndBar = (cb) ->
  waiter (wait) ->
    doFoo wait()
    doBar wait()
  cb
 
# Wait for all update() operations to finish 
updateUsers = (user_ids, data, cb) ->
  waiter (wait) ->
    user_ids.forEach (user_id) ->
      User.update user_iddatawait()
  cb
 
# The wait() function can delegate to a provided callback, 
# and the `this` context can be used to pass data to the callback 
loadUserWithFriends = (user_id, cb) ->
  waiter (wait) ->
    User.loadUser user_idwait (user) -> @data = user.data
    User.loadFriends user_idwait (@friends) ->
  (err, ctx) ->
    if err? then cb err
    else cb nullctx.datactx.friends
 

License

MIT

Dependencies (1)

Dev Dependencies (2)

Package Sidebar

Install

npm i async-waiter

Weekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • nadav