如何传递参数信息异步队列饱和或不饱和?

时间:2019-06-15 16:20:08

标签: javascript node.js

pom.xml

运行代码时出现错误: <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency>

我不确定如何将module.exports = Client; function Client (info) { this.concurrency = info.concurrency; this.queue = async.queue(data, this.concurrency); this.logger = info.logger; this.queue.saturated = function() { this.logger.info('The queue has been saturated'); } this.messagesQueue.unsaturated = function() { this.logger.info('The queue is unsaturated'); } }; 传递给饱和和不饱和方法?

1 个答案:

答案 0 :(得分:1)

thissaturated中的unsaturated指向函数本身。

所以你可以做三件事,

1)。在您的客户端类中,设置一个指向this的变量并使用它。

function Client (info) {
    const that = this;
    this.queue.saturated = function() {
        that.logger.info('The queue has been saturated');
    }
};

2):绑定this:您可以将函数内的this绑定到this上下文的Client

function Client (info) {
    this.queue.saturated = (function() {
        this.logger.info('The queue has been saturated');
    }).bind(this);
};

3):使用箭头功能:箭头功能不绑定this。使用箭头函数声明时,将其读取为thissaturated函数中的 IS NO unsaturated变量。因此,当您从函数内部访问this时,它就像简单的闭包变量一样,将Client用作this

function Client (info) {
    this.queue.saturated = () => {
        this.logger.info('The queue has been saturated');
    }
};