allencoded allencoded - 6 months ago 15
Javascript Question

Bluebird Error: Generator Function Must Be A Function

I am getting the error:

bluebird.js:2118 Uncaught TypeError: generatorFunction must be a function


But I don't understand why this error is occurring, as I am passing it a generatorFunction.

export class Welcome {
fetch() {
Promise.coroutine(this.userData());
}

userData = function* getData() {
this.lotsData = yield this.testApi.getMock();
this.lotsData = JSON.stringify(this.lotsData, null, 4);
}
}


So a click event calls fetch() and that calls this.userData(). This is the console dump of this.userData:

GeneratorFunctionPrototype {}
_invoke: invoke(method, arg)
__proto__: GeneratorFunctionPrototype


Which tells me it most certainly is a generator. I am using all of this in an aurelia class if that somehow makes any difference (which it shouldn't I don't think). So why is it erroring that the generatorFuction must be a function?

Answer

The limited amount of code you posted is riddled with errors, but there is not enough to know what is correct and what is incorrect.

One thing is for sure this.userData() is a function call and not a reference to a function, which is what .coroutine() is expecting.

Whatever type returned by this.testApi.getMock() is what is being yielded, which we have no idea given what you posted, but that is probably not even relevant at this point, because what is returned is probably unknown/null anyway because you are assigning the yield to a variable of questionable scope. Your code is nowhere near the example from the documentation

PingPong.prototype.ping = Promise.coroutine(function* (val) {
    console.log("Ping?", val)
    yield Promise.delay(500)
    this.pong(val+1)
});

The documentation has a very clear and well defined example you should follow, explicitly until you understand what you are actually doing.

Comments