javascript - NodeJS 生成器永远不会到达某一行?

标签 javascript node.js ecmascript-6 generator co

我正在使用 co 运行一个生成器函数,该函数对数据进行一些抓取和清理。但是,我在循环后从未到达代码的特定部分。我的代码如下所示:

function*(){
  var url = "mongodb://" + config.host + ":" + config.port + "/" + config.db;
  var db = yield MongoClient.connect( url );
  for( var establishment in [ {"establishment_id": 16} ] ){
    var establishment_type = establishment.id;
    var response = yield getRestaurants( establishment_type );
    var restaurants = JSON.parse( response.body ).restaurants;
    // here we create our restaurants in "saveable" form
    var saveableRestaurants = [];
    for(var i = 0; i < restaurants.length; i++){
        var saveableRestaurant = restaurants[i].restaurant;
        saveableRestaurant._id = restaurants[i].restaurant.R.res_id;
        // Remove unwanted fields
        //... code removed for brevity ...
        // convert cuisine string into its appropriate id form
        var cuisines = saveableRestaurant.cuisines.split(",");
        var ids = [];
        for( var i = 0; i < cuisines.length; i++ ){
            console.log("LOOKING UP ID FOR ", cuisines[i]);
            var match = yield db.collection(CUI).findOne({"cuisine_name":cuisines[i].trim()});
            console.log("ID FOR ", cuisines[i], " IS", match.cuisine_id);
            ids.push( id );
        }
        // I NEVER REACH THIS LINE!!!!
        console.log( "ALL IDS ", ids );
    }
  }
  db.close();
}  

我从来没有到达函数末尾的控制台语句

最佳答案

很可能存在您没有注意到的异步异常。你应该使用

function*() {
  var url = "mongodb://" + config.host + ":" + config.port + "/" + config.db;
  var db = yield MongoClient.connect( url );
  try {
    for (var establishment of [ {"establishment_id": 16} ]) {
      var establishment_type = establishment.id;
      var response = yield getRestaurants( establishment_type );
      var restaurants = JSON.parse( response.body ).restaurants;
      // here we create our restaurants in "saveable" form
      var saveableRestaurants = [];
      for (var i = 0; i < restaurants.length; i++) {
        var saveableRestaurant = restaurants[i].restaurant;
        saveableRestaurant._id = restaurants[i].restaurant.R.res_id;
        // Remove unwanted fields
        //... code removed for brevity ...
        // convert cuisine string into its appropriate id form
        var cuisines = saveableRestaurant.cuisines.split(",");
        var ids = [];
        for (var i = 0; i < cuisines.length; i++) {
          console.log("LOOKING UP ID FOR ", cuisines[i]);
          var match = yield db.collection(CUI).findOne({"cuisine_name":cuisines[i].trim()});
          console.log("ID FOR ", cuisines[i], " IS", match.cuisine_id);
          ids.push( id );
        }
        console.log( "ALL IDS ", ids );
      }
    }
  } finally {
    db.close();
  }
}

另外,不要忘记在 co 返回的 Promise 上添加 .catch(e => console.error(e))

关于javascript - NodeJS 生成器永远不会到达某一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35846839/

相关文章:

javascript - 如何拦截或编辑柯里化(Currying)函数

Javascript/Node.js 数组内存管理

javascript - ECMAScript 2015 很好的包装 CasperJS 方法的方法。

ecmascript-6 - 使用 let/block 作用域的实际优势是什么?

javascript - sails.js:如何禁用 etag

javascript - 使用动态创建的 src 路径在引导表行的单元格内显示图像

javascript - 如何检查数组是否包含 TypeScript 中的字符串?

javascript - 如何在 Meteor 中使用嵌入文档

javascript - Meteor 应用程序未找到 'xhr-sync-worker.js'?

ruby-on-rails - 将 es6 与 rails Assets 管道一起使用的最佳方法是什么