Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
}

Promise.coroutine(function*(){
    var c = new CompanyController();
    Promise.promisifyAll(c);    
    var res = yield c.someAsyncMethodAsync();
    console.log('never reached');
})();
like image 302
AyKarsi Avatar asked Sep 15 '26 00:09

AyKarsi


1 Answers

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).

like image 190
robertklep Avatar answered Sep 17 '26 13:09

robertklep