异步调用生成“错误:即使使用 _wrapAsync 也不能等待没有“光纤" [英] Async call generates " Error: Can't wait without a "fiber", even with _wrapAsync

查看:39
本文介绍了异步调用生成“错误:即使使用 _wrapAsync 也不能等待没有“光纤"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在meteor 中使用RSS 解析器时遇到问题.这是一个异步调用,所以它不需要被包装,但它似乎仍然不起作用.我认为这是因为匿名 on('readable' 函数在纤程之外,但我不知道如何解决它.

I've been having a problem using an RSS parser in meteor. It's an async call, so it needs ot be wrapped, however it still doesn't seem to work. I presume this is because the anonymous on('readable' function is outside the fiber, but I can't see how to resolve it.

var FeedParser = Meteor.require('feedparser');
var request = Meteor.require('request');

function getBlog(url, parameter, id){

    request(parameter)
        .on('error', function (error) {
            console.error(error);
        })
        .pipe(new FeedParser())
        .on('error', function (error) {
            console.error(error);
        })

        .on('readable', function () {
            var stream = this,
                item;
            while (item = stream.read()) {
                Items.insert(new_item);                
            }
        });
    }

var wrappedGetBlog = Meteor._wrapAsync(getBlog);


Meteor.methods({
    blog: function (url, parameter, id) {
        console.log('parsing blog');
        var items = wrappedGetBlog(url, parameter, id);
    }
});

推荐答案

Meteor._wrapAsync() 期望被包装的函数将错误和结果返回给回调.您的函数 getBlog() 没有这样做,因此 _wrapAsync 不是正确的方法.

Meteor._wrapAsync() expects the wrapped function to return error and result to a callback. Your function, getBlog(), does not do that so _wrapAsync is not the right approach.

我之前已经封装了该函数之前但使用过一个未来.

I have wrapped that function before but used a Future.

这种方法允许我从 Meteor.method() 调用 feedparser,它不允许异步函数,但你也试图在里面做一个 insert可读事件.我认为 insert 会抱怨如果它不在光纤中.也许像这样也是必要的:

That approach allowed me to call feedparser from a Meteor.method(), which doesn't allow async functions, but you are also trying to do an insert inside the readable event. I think that insert will complain if it is not in a fiber. Maybe like this would also be necessary:

var r = request( parameter );
r.on( 'response' , function(){

  var fp = r.pipe( new FeedParser() );  //need feedparser object as variable to pass to bindEnvironment 

  fp.on('readable', Meteor.bindEnvironment( 
    function () {
      var stream = this,
        item;
      while (item = stream.read()) {         
        Items.insert(new_item);                
      }
    }
    , function( error ){ console.log( error );}
    , fp // variable applied as `this` inside call of first function 
  ));
});

<小时>

纤维是另一种选择...


Fibers is another option...

var Fiber = Npm.require( "fibers" );

 .on('readable', function () {
    var stream = this,
        item;
    while (item = stream.read()) {         
        Fiber( function(){
          Items.insert(new_item);
          Fiber.yield();
        }).run();                
    }
});

这篇关于异步调用生成“错误:即使使用 _wrapAsync 也不能等待没有“光纤"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆