0

I'm using the latest co-module (4.6).

This is a Koa-middleware. Therefore it is already co() wrapped.

create: function * () {
  try {
    this.body = yield services.createIt({obj: true})
  } catch (err) {
    this.body = { "errors": err.details }
    this.status = err.status
  }
}

It is calling another generator-function I'm manually wrapping with co:

const co = require('co')

createIt: co(function * (obj) {
  console.log(obj) // --> undefined
}

Why do I "loose" the parameter?

Hedge
  • 16,142
  • 42
  • 141
  • 246
  • You probably meant to use `co.wrap` instead of `co` on `createIt`, but it's hard to tell from the many syntax errors in that snippet – Bergi Aug 05 '16 at 03:59

1 Answers1

1

The function co immediately executes the given generator function with the async/await semantics. If you are just using it from a Koa-middleware, you don't need to wrap the createIt function with co, or you can just use co.wrap to turn the generator into a function that returns a promise (delayed promise). Check https://github.com/tj/co/blob/master/index.js#L26

create: function * () {
  try {
    this.body = yield services.createIt({obj: true})
  } catch (err) {
    this.body = { "errors": err.details }
    this.status = err.status
  }
}

services.js

const co = require('co')

createIt: function * (obj) {
  console.log(obj)
}

// OR

createIt: co.wrap(function *(obj) {
  console.log(obj);
});
zeronone
  • 2,912
  • 25
  • 28