11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst { AsyncLocalStorage } = require('async_hooks');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// This test verifies that async local storage works with thenables
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst store = new AsyncLocalStorage();
111cb0ef41Sopenharmony_ciconst data = Symbol('verifier');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst then = common.mustCall((cb) => {
141cb0ef41Sopenharmony_ci  assert.strictEqual(store.getStore(), data);
151cb0ef41Sopenharmony_ci  setImmediate(cb);
161cb0ef41Sopenharmony_ci}, 4);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cifunction thenable() {
191cb0ef41Sopenharmony_ci  return {
201cb0ef41Sopenharmony_ci    then,
211cb0ef41Sopenharmony_ci  };
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// Await a thenable
251cb0ef41Sopenharmony_cistore.run(data, async () => {
261cb0ef41Sopenharmony_ci  assert.strictEqual(store.getStore(), data);
271cb0ef41Sopenharmony_ci  await thenable();
281cb0ef41Sopenharmony_ci  assert.strictEqual(store.getStore(), data);
291cb0ef41Sopenharmony_ci});
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci// Returning a thenable in an async function
321cb0ef41Sopenharmony_cistore.run(data, async () => {
331cb0ef41Sopenharmony_ci  try {
341cb0ef41Sopenharmony_ci    assert.strictEqual(store.getStore(), data);
351cb0ef41Sopenharmony_ci    return thenable();
361cb0ef41Sopenharmony_ci  } finally {
371cb0ef41Sopenharmony_ci    assert.strictEqual(store.getStore(), data);
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci});
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci// Resolving a thenable
421cb0ef41Sopenharmony_cistore.run(data, () => {
431cb0ef41Sopenharmony_ci  assert.strictEqual(store.getStore(), data);
441cb0ef41Sopenharmony_ci  Promise.resolve(thenable());
451cb0ef41Sopenharmony_ci  assert.strictEqual(store.getStore(), data);
461cb0ef41Sopenharmony_ci});
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci// Returning a thenable in a then handler
491cb0ef41Sopenharmony_cistore.run(data, () => {
501cb0ef41Sopenharmony_ci  assert.strictEqual(store.getStore(), data);
511cb0ef41Sopenharmony_ci  Promise.resolve().then(() => thenable());
521cb0ef41Sopenharmony_ci  assert.strictEqual(store.getStore(), data);
531cb0ef41Sopenharmony_ci});
54