sails.js 将ES2015生成器与co.js结合使用

示例

'use strict';

const co = require('co');

module.exports = {
  // 这是索引操作,路由通过/config/routes.js映射
  index(req, res) {
    co(function* index() {
      // 返回没有视图模型数据的视图
      // This typically will return the view defined at /views/home/index.<view engine extension>
      return res.view();
    }).catch(res.negotiate); // 捕获所有抛出的错误,并将错误传递给“协商”策略。
  },
  foo(req, res) {
    co(function* foo() {
      // 从数据库中获取`FooBar`项目的数组
      const items = yield FooBar.find();

      // 使用包含“ FooBar”项数组的视图模型返回“ foo”视图
      return res.view({
        items,
      });
    }).catch(res.negotiate); // 捕获所有抛出的错误,并将错误传递给“协商”策略。
  },
};