caolan / highland

High-level streams library for Node.js and the browser

Home Page:https://caolan.github.io/highland

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I think that toPromise is taking a wrong value

cocodrino opened this issue · comments

Hi good day, I've a class named RPC, here we've this method

  public async send(nber : number) : Promise<ConsumeMessage> {
    if (!this.stream) {
      await this.delay(2);
      console.info('RPC not initialized yet...retrying');
      await this.send(nber);
    }

    const correlationId = RPC.generateUuid();
    this.channel?.sendToQueue('rpc_queue', Buffer.from(nber.toString()), {correlationId, replyTo: this.queue?.queue});
    return this.stream?.find((msg : ConsumeMessage|null) => {
      return msg?.properties.correlationId === correlationId;
    }).toPromise(Promise);
  }

when I try to run this code I get this message:

Unhandled rejection TypeError: self._rpc is not a function
    at /Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/amqplib/lib/channel_model.js:171:10
    at tryCatcher (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/bluebird/js/release/util.js:16:23)
    at Function.Promise.fromNode.Promise.fromCallback (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/bluebird/js/release/promise.js:209:30)
    at C.consume (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/amqplib/lib/channel_model.js:170:18)
    at Stream._generator (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:4938:15)
    at Stream._runGenerator (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:1354:10)
    at Stream._resume (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:1188:22)
    at Stream._checkBackPressure (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:1101:10)
    at Stream._resume (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:1182:33)
    at Stream._checkBackPressure (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:1101:10)
    at Stream._resume (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:1182:33)
    at Stream._checkBackPressure (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:1101:10)
    at Stream._resume (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:1182:33)
    at Stream.resume (/Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:1216:10)
    at /Users/Admin/Downloads/proj/avanti/bita_dashboard_websocket_backend/node_modules/highland/lib/index.js:2146:13
    at new Promise (<anonymous>)


very weird that self._rpc is not a function because I don't have anything like that in my code, after check the code seems that I'm not passing the right PromiseCtor but I don't know what must I pass to this method

Stream.prototype.toPromise = function (PromiseCtor) {
    var self = this;
    return new PromiseCtor(function(resolve, reject) {
        self.consume(toCallbackHandler('toPromise', function(err, res) {
            if (err) {
                reject(err);
            }
            else {
                resolve(res);
            }
        })).resume();
    });
};
exposeMethod('toPromise');

I even try pass a bluebird promise but I get the same message: self._rpc is not a function

do you know what am I doing wrong?...thank you so much!!!

Hi @cocodrino,

Might it be that the Promise in .toPromise(Promise) is not global.Promise? Maybe something is shadowing it.