Meteor wrapAsync同步执行,但从不返回

我试图用wrapAsync封装一个节点包中的函数。

filepicker = new Filepicker('API Key') filepickerStatSync = Meteor.wrapAsync(filepicker.stat, filepicker) result = filepickerStatSync(url); console.log('after') 

stat函数如下。

一切似乎工作正常…请求调用响应正确的结果,最后的callback被调用,整个事情同步执行/产量正确,据我所知…但同步调用永远不会返回和console.log ('之后')从未被击中。

我不认为我犯了同样的错误发生在这个问题上,因为我的函数有一个callback作为最后一个参数。

我也不认为解决scheme是在这个问题上,因为包装函数结束与调用错误和结果的callback,这应该是什么Meteor.wrapAsync在签名中寻找。

 Filepicker.prototype.stat = function(url, options, callback) { callback = callback || function(){}; if(!options) { options = {}; } if(!url) { callback(new Error('Error: no url given')); return; } request({ method: 'GET', url: url+'/metadata?', form: { size: options.size || true, mimetype: options.mimetype || true, filename: options.filename || true, width: options.width || true, height: options.height || true, writeable: options.writeable || true, md5: options.md5 || true, path: options.path || true, container: options.container || true, security: options.security || {} } }, function(err, res, body) { console.log('err = '+err); console.log('res = '+res); console.log('body = '+body); if(err) { callback(err); return; } var returnJson; if(typeof(body)==='string'){ try { returnJson = JSON.parse(body); } catch(e) { callback(new Error('Unknown response'), null, body); return; } } else { console.log('returnJSON'); returnJson = body; } console.log('callbacked'); callback(null, returnJson); }); }; 

你正在包装的函数有三个参数,但是你只提供了两个: url和(隐式地)callback函数(我将它称为cb )。 所以在内部,执行的是Filepicker.prototype.stat(url, cb) ,即callback函数cb将被解释为options而不是callbackcallback函数将被设置为空函数。 所以wrapAsync的callback从不被调用,因为callback链被破坏了。

这应该工作:

 result = filepickerStatSync(url, {});