amit_183
amit_183

Reputation: 981

Nodejs async queue (TypeError: Cannot assign to read only property 'drain' of object '#<Object>')

Following is the queueClass.js :

const async = require('async');
export default class queueClass {
  constructor(requestObj) {
    this.queueMaxConnections = requestObj['queueMaxConnections'] || 10;
    this.queueMaxLength = requestObj['queueMaxLength'] || 4;
    
    this.msgQueue = async.queue(async (data, done) => {
        await this.handleCB(data, this.onData);
         done(data);
    }, 2); // queue maxparallelhandles -- concurrency

    this.msgQueue.drain = async () => {
        // some callback
    };
    
  }
}

I am importig this file in helper.js:

import queueClass from './queueClass'
var requestObj = {
 'queueMaxConnections': 5,
 'queueMaxLength':10
}
var queueVar = new queueClass(requestObj);
/* getting error in above line */

Error: TypeError: Cannot assign to read only property 'drain' of object '#<Object>'

Can anybody tell me why it is happening and the possible solution for it.

Upvotes: 0

Views: 938

Answers (2)

Corin
Corin

Reputation: 2467

As pointed out, the drain property is not writable so you cannot just assign a new function to it. However, the async queue documentation shows that you can pass your function as a callback to drain. In your case that looks like:

this.msgQueue.drain(async () => {
    // your drain code here
};

Upvotes: 3

Vadim Liakhovich
Vadim Liakhovich

Reputation: 116

I looked at the async repository code and I think it happens because async.queue has drain property writable: false. Please refer to https://github.com/caolan/async/blob/master/lib/internal/queue.js#L261

Upvotes: 1

Related Questions