Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor wrapAsync executes synchronously but never returns

I'm trying to wrap a function from a node package with wrapAsync.

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

The stat function is below.

Everything seem to work fine... the request call responds with the correct result, the final callback is called, the whole thing executes synchronously / yields correctly as far as I can tell... but the sync call never returns and console.log('after') is never hit.

I don't think I made the same mistake as happened in this question because my function has a callback as the last parameter.

I also don't think the solution is in this question because the wrapped function does end with calling the callback with error and result, which is supposedly what Meteor.wrapAsync is looking for in a signature.

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);
    });
};
like image 330
funkyeah Avatar asked Oct 31 '22 20:10

funkyeah


1 Answers

The function you are wrapping takes three arguments, but you are only providing two: url, and (implicitly) the callback function (I'll call it cb). So internally, what will get executed is Filepicker.prototype.stat(url, cb), i.e., the callback function cb will get interpreted as the options rather than the callback, and callback will be set to an empty function. So the callback of wrapAsync is never called, because the callback chain was broken.

This should work:

result = filepickerStatSync(url, {});
like image 128
Christian Fritz Avatar answered Nov 12 '22 18:11

Christian Fritz