Skip to content

Commit f931b9d

Browse files
sam-githubFishrock123
authored andcommitted
timer: ref/unref return self
Most calls to ref() and unref() are chainable, timers should be chainable, too. Typical use: var to = setTimeout(ontimeout, 123).unref(); PR-URL: #2905 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 4b4cfa2 commit f931b9d

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

doc/api/timers.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ In the case of `setTimeout` when you `unref` you create a separate timer that
4141
will wakeup the event loop, creating too many of these may adversely effect
4242
event loop performance -- use wisely.
4343

44+
Returns the timer.
45+
4446
## ref()
4547

4648
If you had previously `unref()`d a timer you can call `ref()` to explicitly
4749
request the timer hold the program open. If the timer is already `ref`d calling
4850
`ref` again will have no effect.
4951

52+
Returns the timer.
53+
5054
## setImmediate(callback[, arg][, ...])
5155

5256
To schedule the "immediate" execution of `callback` after I/O events

lib/timers.js

+3
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,13 @@ Timeout.prototype.unref = function() {
330330
this._handle.domain = this.domain;
331331
this._handle.unref();
332332
}
333+
return this;
333334
};
334335

335336
Timeout.prototype.ref = function() {
336337
if (this._handle)
337338
this._handle.ref();
339+
return this;
338340
};
339341

340342
Timeout.prototype.close = function() {
@@ -345,6 +347,7 @@ Timeout.prototype.close = function() {
345347
} else {
346348
exports.unenroll(this);
347349
}
350+
return this;
348351
};
349352

350353

test/parallel/test-timers-unref.js

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ var interval_fired = false,
1212
var LONG_TIME = 10 * 1000;
1313
var SHORT_TIME = 100;
1414

15+
assert.doesNotThrow(function() {
16+
setTimeout(function() {}, 10).unref().ref().unref();
17+
}, 'ref and unref are chainable');
18+
19+
assert.doesNotThrow(function() {
20+
setInterval(function() {}, 10).unref().ref().unref();
21+
}, 'ref and unref are chainable');
22+
1523
setInterval(function() {
1624
interval_fired = true;
1725
}, LONG_TIME).unref();

0 commit comments

Comments
 (0)