co.wrap yield Promise throw TypeError: undefined is not a function

1.2k Views Asked by At

I yleld a Promises in co.wrap, but it throw TypeError: undefined is not a function (the error line is yield pm2.connect();)

app.js:

var app = koa();
app.init = co.wrap(function *(overwriteDB) {

  yield pm2.connect();

    koaConfig(app);

    app.server = app.listen(config.app.port);
      if (config.app.env !== 'test') {
        console.log('PM2 monitor listening on port ' + config.app.port);
      }
    });
  }

  if (!module.parent) {
    app.init().catch(function (err) {
      console.error(err.stack);
      process.exit(1);
    });
  }

In pm2.js, I wrapped a fuction to return a Promise, the code is below:

var _ = PM2.prototype;

exports = module.exports = PM2;

function PM2() {
  debug("PM2");
  if (!(this instanceof PM2)) return new PM2;
  this.env = process.env.NODE_ENV || 'development';
};

_.connect = function() {
  debug('connect');
  return new Promise(function(resolve, reject) {
    pm2.connect(function(err) {
    if (err) reject(err);
    resolve();
  });
});

Then I try

co(function *() {
  yield pm2.connect();
  var res = yield pm2.list();
  console.log(res);
});

in app.js, it works OK.

1

There are 1 best solutions below

0
On BEST ANSWER

I forgot ()

require('./lib/pm2')();