为什么这个咖啡代码总是返回true?

当我发布到服务器,无论我给auth函数的信息,它返回true。 我的直觉是,我正在尝试做同步的事情,这本质上是asynchronous的,但我不知道如何解决它。

auth = (username, api_key, device) -> hashed_key = hash.sha256(username + api_key + device, salt) winston.debug('Checking auth for ' + username) redis_client.get hashed_key, (err, data) -> if data == username true # Main Handler for posting data for a device. server.post "/:customer/:site/:device", create = (req, res, next) -> message = JSON.parse(req.body) winston.info(server.name + ': Recieved event from ' + req.params.device) authenticated = auth(message.username, message.api_key, message.device) winston.debug('****' + authenticated) if authenticated == true winston.debug('Auth passed, got a valid user/device/api combination: ' + message.username) redis_client.publish('device_events', req.body) return next() else winston.debug('Auth failed, cant find device ' + message.device + ' for ' + message.username) return next(restify.NotAuthorizedError) 

如果你知道(或者有一种预感)某种东西是asynchronous的,那么你应该把做后续的事情作为callback函数。 我不知道你的服务器的post函数是如何工作的,但是如果它像Node HTTP的请求,你应该做如下的事情:

 get = (location, callback, retriever, filterer, formatter)-> decoratedCallback = (data)-> callback formatter.applyFormat filterer.applyFilter data retriever.retrieve location, decoratedCallback module.exports = get