1

私は最近 ES6 を少し学び、io.js で実行される koa.js を使用して新しいプロジェクトを開始しました。

以下のコードでは、同じurl slug.

しかし、の値はcounter.next().value常に関数を返すため、関数_slugExistsは常に戻りますtrue

'use strict';

let _ = require('lodash');
let config = require('../../config');

let monk = require('monk');
let wrap = require('co-monk');
let db = monk(config.db);

let _apps = wrap(db.get('apps'));

function _validateApp(app) {
  let slug = app.slug.trim();

  if (!app.name.trim()) {
    throw new Error('App name was not specified.');
  }

  if (!slug) {
    throw new Error('App URL was not specified.');
  }

  if (_slugExists(slug)) {
    throw new Error('Another app with the same URL already exists.');
  }
}

function* _count(filter) {
  yield _apps.count(filter);
}

function _slugExists(slug) {
  let counter = _count({
    slug: slug
  });

  return counter.next().value !== 0;
}

module.exports = {
  list: function*(next) {
    this.status = 200;
    this.body = yield _apps.find({});
  },
  create: function*(next) {
    try {
      let app = this.request.body;
      _validateApp(app);

      this.status = 201;
      this.body = {
        id: yield _apps.insert({
          name: app.name.trim(),
          slug: app.slug.trim(),
          created_at: new Date()
        })
      };
    } catch (error) {
      console.log(`[ERROR] ${error.message}`);

      this.status = 500;
      this.body = {
        error: error.message
      };
    }
  }
}
4

1 に答える 1

2

coに基づくkoaでは、すべての非同期操作はkoa まですべてを約束する必要があります。ジェネレーターもできますが、イテレーターはできません。特に重要なのは、ネストされた非同期操作がハングしたままにならないようにすることです。yieldyield

function* middleware(next) {
  yield Promise.resolve(0); // Yielding a promise. Good.
  yield (function() { return Promise.resolve(0); })(); // Also yielding a promise. Good.

  yield gen(4); // Yielding iterator. NOT GOOD!
  yield gen; // Yielding generator. Good, but no arg.
  yield* gen(4); // Delegating iterator. Good!

  hangingNested(); // Not yielding anything, async is lost. NOT GOOD!
  yield properNested; // Yielding generator with nested delegate, good!
}

function* gen(arg) {
  yield Promise.resolve(1);
  yield Promise.resolve(2);
  yield Promise.resolve(3);
  return arg;
}

function hangingNested() { // not a generator, nothing is yielded outside.
  gen(4); // iterator is lost.
}

function* properNested() {
  yield* gen(4); // Delegating iterator.
}

それを念頭に置いて、コードを修正するさまざまな方法があります。たとえば、次のようになります。

function* _validateApp(app) {
  let slug = app.slug.trim();

  if (!app.name.trim()) {
    throw new Error('App name was not specified.');
  }

  if (!slug) {
    throw new Error('App URL was not specified.');
  }

  if (yield* _slugExists(slug)) {
    throw new Error('Another app with the same URL already exists.');
  }
}

function* _count(filter) {
  return yield _apps.count(filter);
}

function* _slugExists(slug) {
  let counter = yield* _count({
    slug: slug
  });

  return counter !== 0;
}

module.exports = {
  list: function*(next) {
    this.status = 200;
    this.body = yield _apps.find({});
  },
  create: function*(next) {
    try {
      let app = this.request.body;
      yield* _validateApp(app);

      this.status = 201;
      this.body = {
        id: yield _apps.insert({
          name: app.name.trim(),
          slug: app.slug.trim(),
          created_at: new Date()
        })
      };
    } catch (error) {
      console.log(`[ERROR] ${error.message}`);

      this.status = 500;
      this.body = {
        error: error.message
      };
    }
  }
}
于 2015-10-12T07:26:14.823 に答える