summaryrefslogtreecommitdiff
path: root/test/parallel/test-async-hooks-promise-triggerid.js
blob: b860d60999e1ef27961f282fa342f84ce2430d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';
const common = require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');

if (!common.isMainThread)
  common.skip('Worker bootstrapping works differently -> different async IDs');

const promiseAsyncIds = [];

async_hooks.createHook({
  init: common.mustCallAtLeast((id, type, triggerId) => {
    if (type === 'PROMISE') {
      // Check that the last known Promise is triggering the creation of
      // this one.
      assert.strictEqual(triggerId,
                         promiseAsyncIds[promiseAsyncIds.length - 1] || 1);
      promiseAsyncIds.push(id);
    }
  }, 3),
  before: common.mustCall((id) => {
    assert.strictEqual(id, promiseAsyncIds[1]);
  }),
  after: common.mustCall((id) => {
    assert.strictEqual(id, promiseAsyncIds[1]);
  })
}).enable();

Promise.resolve(42).then(common.mustCall(() => {
  assert.strictEqual(async_hooks.executionAsyncId(), promiseAsyncIds[1]);
  assert.strictEqual(async_hooks.triggerAsyncId(), promiseAsyncIds[0]);
  Promise.resolve(10);
}));