如何在JavaScript中将Promise添加到事件处理程序 [英] How to add Promise to event handler in javascript

查看:100
本文介绍了如何在JavaScript中将Promise添加到事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我想将 amqp 包装为Promise

Now I want to wrap amqp with Promise Q, here are the codes

Sender.prototype.createConnection_ = function () {
    var deferred = Q.defer();
    this.con_ = amqp.createConnection( this.connectOpt_, this.implementOpt_ );
    deferred.resolve( this.con_ );

    return deferred.promise;
}

Sender.prototype.connectionReady_ = function() {
    var deferred = Q.defer(),
      self = this;

    self.con_.on('ready', function() {
        console.log('connection is ok now');
        deferred.resolve(self.con_);
    });
    return deferred.promise;
}

Sender.prototype.createExchange_ = function() {
    var deferred = Q.defer(),
      self = this;

    this.con_.exchange( this.exchangeName_, this.exchangeOpt_, function ( ex ) {
        self.ex_ = ex;
        deferred.resolve(self.ex_);
    });
    return deferred.promise;
}

Sender.prototype.exchangeReady_ = function() {
    var deferred = Q.defer(),
      self = this;

    this.ex_.on('open', function() {
        console.log('Sender: exchange opened');
        deferred.resolve(this.ex_);
    });
    return deferred.promise;
}

Sender.prototype.connect_ = function() {
    var self = this;
    return self.createConnection_()
            .then( self.connectionReady_() )
            .then( self.createExchange_() )
            .then( self.exchangeReady_() )
            .catch( function(err) {
                console.info( err );
            });
}

当我想调用connect_时,在exchangeReady_函数中有一个错误表明this.ex_null.

When I want to invoke connect_, there is one error show that this.ex_ is null in the exchangeReady_ function.

我想如何在事件openready函数中添加Q?

I want to how to add Q in the event open and ready function ?

推荐答案

您要立即调用函数,而不是将函数引用传递给.then()处理程序. .then()将函数引用而不是promise作为参数.更改为此:

You're calling your functions immediately, rather than passing a function reference to the .then() handlers. .then() takes a function reference, not a promise as an argument. Change to this:

Sender.prototype.connect_ = function() {
    return this.createConnection_()
            .then( this.connectionReady_.bind(this) )
            .then( this.createExchange_.bind(this) )
            .then( this.exchangeReady_.bind(this) )
            .catch( function(err) {
                console.info( err );
            });
}

.bind(this)使您可以传递函数引用(.then()基础结构可以稍后调用的东西),并且仍将其绑定到this.

The .bind(this) lets you pass a function reference (something the .then() infrastructure can call later) and still have it bound to this.

当您传递这样的回调时,您似乎还可能遇到绑定问题:

It looks like you may also have binding issues when you pass callbacks like this:

amqp.createConnection( this.connectOpt_, this.implementOpt_ );

这些回调将不会绑定到this.而是在方法为以下任何回调上使用.bind()这样:

These callbacks will not stay bound to this. Instead, use .bind() like this on any callback that is method:

amqp.createConnection( this.connectOpt_.bind(this), this.implementOpt_.bind(this) );

同一问题在您的代码中还存在其他几个地方.

The same issue exists several other places in your code.

这篇关于如何在JavaScript中将Promise添加到事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆