• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const async_hooks = require('async_hooks');
5
6if (!common.isMainThread)
7  common.skip('Worker bootstrapping works differently -> different async IDs');
8
9const promiseAsyncIds = [];
10
11async_hooks.createHook({
12  init: common.mustCallAtLeast((id, type, triggerId) => {
13    if (type === 'PROMISE') {
14      // Check that the last known Promise is triggering the creation of
15      // this one.
16      assert.strictEqual(triggerId,
17                         promiseAsyncIds[promiseAsyncIds.length - 1] || 1);
18      promiseAsyncIds.push(id);
19    }
20  }, 3),
21  before: common.mustCall((id) => {
22    assert.strictEqual(id, promiseAsyncIds[1]);
23  }),
24  after: common.mustCall((id) => {
25    assert.strictEqual(id, promiseAsyncIds[1]);
26  })
27}).enable();
28
29Promise.resolve(42).then(common.mustCall(() => {
30  assert.strictEqual(async_hooks.executionAsyncId(), promiseAsyncIds[1]);
31  assert.strictEqual(async_hooks.triggerAsyncId(), promiseAsyncIds[0]);
32  Promise.resolve(10);
33}));
34