meteor-collection-hooks icon indicating copy to clipboard operation
meteor-collection-hooks copied to clipboard

Do after hooks block?

Open lorensr opened this issue 9 years ago • 3 comments

After a user is created, I want the client's Accounts.createUser call to return, and then I want to add something to the user's record that is fetched with a HTTP request that is wrapAsynced. Can I put that HTTP request in Meteor.users.after.insert, or would that block the createUser returning?

lorensr avatar Mar 24 '15 06:03 lorensr

Have you given it a try?

matb33 avatar Apr 14 '15 19:04 matb33

Just did this, which blocks createUser returning.

Meteor.users.after.insert (x, doc) ->
  l 'after insert'
  a = (cb) ->
    setTimeout ->
      cb()
      l 'timeout'
    , 5000
  block = Meteor.wrapAsync a
  block()
  l 'end'

Just calling Meteor.setTimeout does not block, so I guess you should wrap things that take long and aren't needed at return time:

Meteor.users.after.insert (x, doc) ->
  Meteor.setTimeout =>
    console.log @_id
    apiCall()
  , 0

Perhaps worth noting in docs?

Also, using _.defer doesn't work, gives:

[Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.]

lorensr avatar Apr 14 '15 23:04 lorensr

(I'm assuming methods other than createUser that insert docs that have hooks similarly block on the [insert+insert hook] returning.)

lorensr avatar Apr 14 '15 23:04 lorensr