• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5
6const immediate = setImmediate(() => {});
7assert.strictEqual(immediate.hasRef(), true);
8immediate.unref();
9assert.strictEqual(immediate.hasRef(), false);
10clearImmediate(immediate);
11
12// This immediate should execute as it was unrefed and refed again.
13// It also confirms that unref/ref are chainable.
14setImmediate(common.mustCall(firstStep)).ref().unref().unref().ref();
15
16function firstStep() {
17  // Unrefed setImmediate executes if it was unrefed but something else keeps
18  // the loop open
19  setImmediate(common.mustCall()).unref();
20  setTimeout(common.mustCall(() => { setImmediate(secondStep); }), 0);
21}
22
23function secondStep() {
24  // clearImmediate works just fine with unref'd immediates
25  const immA = setImmediate(() => {
26    clearImmediate(immA);
27    clearImmediate(immB);
28    // This should not keep the event loop open indefinitely
29    // or do anything else weird
30    immA.ref();
31    immB.ref();
32  }).unref();
33  const immB = setImmediate(common.mustNotCall()).unref();
34  setImmediate(common.mustCall(finalStep));
35}
36
37function finalStep() {
38  // This immediate should not execute as it was unrefed
39  // and nothing else is keeping the event loop alive
40  setImmediate(common.mustNotCall()).unref();
41}
42