From 8eb2b6c61f9fdac12f8bab23ad3e9ef71c7c59b1 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Tue, 2 Apr 2024 17:53:29 -0500 Subject: feat: improve AsyncLocalStorage api (#23175) Fixes: https://github.com/denoland/deno/issues/23174 --- tests/unit_node/async_hooks_test.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests/unit_node') diff --git a/tests/unit_node/async_hooks_test.ts b/tests/unit_node/async_hooks_test.ts index 076d14b54..2ed197c2d 100644 --- a/tests/unit_node/async_hooks_test.ts +++ b/tests/unit_node/async_hooks_test.ts @@ -94,3 +94,34 @@ Deno.test(async function enterWith() { assertEquals(await deferred.promise, { x: 2 }); assertEquals(await deferred1.promise, { x: 1 }); }); + +Deno.test(async function snapshot() { + const als = new AsyncLocalStorage(); + const deferred = Promise.withResolvers(); + + als.run(null, () => { + const snapshot = AsyncLocalStorage.snapshot(); + als.run({ x: 1 }, () => { + deferred.resolve(snapshot(() => als.getStore())); + }); + }); + + assertEquals(await deferred.promise, null); +}); + +Deno.test(async function bind() { + const als = new AsyncLocalStorage(); + const deferred = Promise.withResolvers(); + + const bound = als.run(null, () => { + return AsyncLocalStorage.bind(() => { + deferred.resolve(als.getStore()); + }); + }); + + als.run({ x: 1 }, () => { + bound(); + }); + + assertEquals(await deferred.promise, null); +}); -- cgit v1.2.3