AyKarsi AyKarsi - 1 year ago 262
Node.js Question

Promisifying node.js class generator method

I'm trying to promisify the the someAsyncMethod in the following code.

When I run the the code, the yielded promise of the someAsyncMethod is never resolved.

Can someone tell me what I'm doing wrong?

'use strict';
var someapi = require('./lib/absenceio');
var Promise = require('bluebird');

class CompanyController {
constructor(currentUser, parameters) {

* someAsyncMethod () {
return yield someapi.listReasons(); // returns a promise

var c = new CompanyController();
var res = yield c.someAsyncMethodAsync();
console.log('never reached');

Answer Source

someAsyncMethod() is a generator function, which you can't yield directly, but you can delegate to it using yield*.

So, your code becomes this:

var res = yield* c.someAsyncMethod();

No need to promisify it (I'm not even sure what Bluebird does when asked to promisify a generator function).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download