【发布时间】:2014-11-26 11:51:24
【问题描述】:
有
文件A
userID = (userName) ->
id = 0
someAPI.getUser userName, (err, data, res) ->
id = data.id if data
console.log id # Outputs ID
return
id
console.log userID('someUsername') # Outputs 0
文件B
getUser: (username, callback) ->
return api.get 'users/show', { 'username': username }, callback
我怎样才能让console.log userID('someUsername') 也输出 ID,而不是 0? IE。在返回 id 之前让它等待。
我尝试过使用 Meteor.wrapAsync 和 Meteor.bindEnvironment 随机包装东西,但似乎无法到达任何地方。
【问题讨论】:
-
把它放在
someAPI.getUser的回调里面。 -
欢迎来到异步的奇妙世界,你不能那样做。查看相关stackoverflow.com/q/23667086/1331430
标签: javascript node.js meteor coffeescript