fun*_*eah 5 javascript asynchronous node.js meteor
我正在尝试使用wrapAsync从节点包中包装一个函数.
filepicker = new Filepicker('API Key')
filepickerStatSync = Meteor.wrapAsync(filepicker.stat, filepicker)
result = filepickerStatSync(url);
console.log('after')
Run Code Online (Sandbox Code Playgroud)
统计功能如下.
一切似乎工作正常...请求调用响应正确的结果,最后的回调被调用,整个事情同步/正确执行,据我所知...但同步调用永远不会返回和console.log ('之后')永远不会被击中.
我不认为我犯了与这个问题相同的错误,因为我的函数有一个回调作为最后一个参数.
我也不认为解决方案是在这个问题中,因为包装函数的确以调用带有错误和结果的回调结束,这应该是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);
});
};
Run Code Online (Sandbox Code Playgroud)
您所包装的函数需要三个参数,但您只提供两个:url和(隐式)回调函数(我将其称为cb)。因此,在内部,将执行的是Filepicker.prototype.stat(url, cb),即回调函数cb将被解释为 ,而options不是callback,并且callback将被设置为空函数。因此,wrapAsync 的回调永远不会被调用,因为回调链被破坏了。
这应该有效:
result = filepickerStatSync(url, {});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
273 次 |
| 最近记录: |