【发布时间】:2015-11-25 21:12:23
【问题描述】:
我正在尝试使用 wrapAsync 包装来自节点包的函数。
filepicker = new Filepicker('API Key')
filepickerStatSync = Meteor.wrapAsync(filepicker.stat, filepicker)
result = filepickerStatSync(url);
console.log('after')
stat 函数如下。
一切似乎都正常...请求调用以正确的结果响应,调用最终回调,就我所知,整个事情同步执行/正确产生...但是同步调用永远不会返回并且console.log('after') 永远不会被命中。
我不认为我犯了与question 相同的错误,因为我的函数有一个回调作为最后一个参数。
我也不认为解决方案在这个question 中,因为包装函数确实以调用带有错误和结果的回调结束,这应该是 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);
});
};
【问题讨论】:
标签: javascript node.js asynchronous meteor