Javascript – calling clearTimeout () does not seem to work

I make a Overwritable over timea small envelope over the class WritableStream Node, add the functionality of Time-out When writing data: If the processing of the data takes a very long time, an event is issued to take appropriate action.

I call with every letter setTimeout (); and in the call write ()I set a callback to clear that Time-out previous

But for some reason, I miss that, though Yes I'm calling clearTimeout (), I enter in the setTimeout ()if it is already deleted.

The code is long, I could not reduce it anymore. Contains the relevant parts of Overwritable over time + the bill what I use for the test.

const {EventEmitter} = required (& # 39; events & # 39;);

Class TimedWritable extends EventEmitter {
get userData () {return. $ user; }
get count () {return this. $ count; }

Constructor (writable, userData) {
Super ();

this. $ queue = {};
this. $ currentId = 0;
this. $ writable = writable;
this. $ user = userData;
this. $ count = 0;
}

$ consumeTimeout (self, id) {
var user
work = self. $ queuehttps://es.stackoverflow.com/q/216301;

// That can not happen.
if (work === undefined) throw error (& # 39; internal error: id undefined & # 39;);

- self. $ count;

// If it has been canceled, there is nothing to do.
if (work === -1) {
Delete yourself. $ queuehttps://es.stackoverflow.com/q/216301;
to return;
}

user = work.user;

// If we had a break for the product, we removed it. It is no longer necessary.
if (self. $ queuehttps://es.stackoverflow.com/q/216301.produce) clearTimeout (itself. $ queuehttps://es.stackoverflow.com/q/216301.produce);

// We remove the work from the queue.
Delete yourself. $ queuehttps://es.stackoverflow.com/q/216301;

self.emit ("consume", "self", "id", "user");
}

// Call when all data to be sent has been consumed.
$ consumeHandler (id) {
var user
work = this. $ queuehttps://es.stackoverflow.com/q/216301;

// This ID will be sent by us in & # 39; send () & # 39; generated.
// If the ID is not, that's because the timeout has been reached before.
// or it has been canceled
// Nothing to do.
if (work === undefined) return;

// We remove the timeout.
if (work.consume) clearTimeout (work.consume);

// If & # 39; Produce & # 39; If there is no timeout, we will remove this job from the queue.
if (! work.produce) {
delete these. $ queuehttps://es.stackoverflow.com/q/216301;
// And we generate the event.
this.with (& # 39 ;, this, id, work.user);
} else {
// We only delete the consumption timeout.
delete work.consume;
}
}

send (consume, produce, chunk, choices)
var id,
job

if (this. $ closed) throw error (& # 39; TimedStream closed & # 39;);

if ((opts == null) || (opts === undefined)) opts = {}

id = ++ this. $ currentId;

++ that. $ count;

if (id == 2147483647) {
this. $ currentId = 1;
id = 1;
}

Work = {
id: id,
};
if (& 39; data & # 39; in opts) work.user = opts.data;

work.consume = setTimeout (this. $ consumeTimeout, consume * 1000, this, id);
if (produced) work.produce = setTimeout (this. $ producesTimeout, generates * 1000, this, id);

these. $ Queuehttps://es.stackoverflow.com/q/216301 = Work;
this. $ writable.write (chunk, encoding in opts? opts.encoding: null, () => this. $ consumeHandler (id));
}
}

Class Writable extends EventEmitter {
Constructor () {super (); }
Write (Chunk, Encoding, Handler)
this. $ chunk = chunk;
this. $ handler = handler;
}
consume () {
this. $ handler ();
}
};

Function consumeTimeout (... args) {
console.log (& # 39; cosumeTimeout & # 39;);
console.log (args);
}

var fake = new Writable;
var tw = new TimedWritable (fake);

tw.on (& # 39; consume, timeout);

tw.send (5, 0, "Hello, world!");

setTimeout (
fake.consume.bind (fake),
3000);

If everything goes right, There should be no exit; The execution should be finished approx. 3 seconds after the start.

however always it means consumeTimeout () {...}displayed on the console

consumption timeout

When Not There should be no way out.

I use:

Node – version
v10.13.0