Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
worker: make calling terminate always return a promise
This change makes terminate always return a promise even if kHandler
is null
  • Loading branch information
elyalvarado authored and Trott committed Jul 30, 2019
commit fd8a6041b105550e46502221bd623b4259f9abaa
5 changes: 2 additions & 3 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,18 @@ class Worker extends EventEmitter {
}

terminate(callback) {
if (this[kHandle] === null) return Promise.resolve();

debug(`[${threadId}] terminates Worker with ID ${this.threadId}`);

if (typeof callback === 'function') {
process.emitWarning(
'Passing a callback to worker.terminate() is deprecated. ' +
'It returns a Promise instead.',
'DeprecationWarning', 'DEP0132');
if (this[kHandle] === null) return;
this.once('exit', (exitCode) => callback(null, exitCode));
}

if (this[kHandle] === null) return Promise.resolve();

this[kHandle].stopThread();

// Do not use events.once() here, because the 'exit' event will always be
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-worker-terminate-null-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const common = require('../common');
const { Worker } = require('worker_threads');

// Test that calling worker.terminate() if kHandler is null should return an
// empty promise that resolves to undefined
// empty promise that resolves to undefined, even when a callback is passed

const w = new Worker(`
const { parentPort } = require('worker_threads');
Expand All @@ -18,8 +18,8 @@ process.once('beforeExit', common.mustCall(() => {

w.on('exit', common.mustCall(() => {
console.log('exit');
assert.strictEqual(w.terminate(() => null), undefined);
w.terminate().then(returned => assert.strictEqual(returned, undefined));
w.terminate(() => null).then(returned => assert.strictEqual(returned, undefined));
}));

w.unref();