meteor-collection-hooks
meteor-collection-hooks copied to clipboard
Do after hooks block?
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?
Have you given it a try?
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.]
(I'm assuming methods other than createUser
that insert docs that have hooks similarly block on the [insert+insert hook] returning.)