温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/async_hooks/AsyncLocalStorage/snapshot
点击访问原文链接

AsyncLocalStorage.snapshot method | Node.js async_hooks module | Bun

AsyncLocalStorage.snapshot method | Node.js async_hooks module | BunBuildDocsReferenceGuidesBlogDiscord/node:async_hooks/AsyncLocalStorage/snapshotMsnapshot

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:async_hooks/AsyncLocalStorage/snapshotMsnapshot

method

async_hooks.AsyncLocalStorage.snapshot AsyncLocalStorage.snapshot()); const result = asyncLocalStorage.run(321, () => runInAsyncScope(() => asyncLocalStorage.getStore())); console.log(result); // returns 123 ``` AsyncLocalStorage.snapshot() can replace the use of AsyncResource for simple async context tracking purposes, for example: ```js class Foo { #runInAsyncScope = AsyncLocalStorage.snapshot(); get() { return this.#runInAsyncScope(() => asyncLocalStorage.getStore()); } } const foo = asyncLocalStorage.run(123, () => new Foo()); console.log(asyncLocalStorage.run(321, () => foo.get())); // returns 123 ```" data-algolia-static="true" data-algolia-merged="false" data-type="Method">static snapshot(): (fn: (...args: TArgs) => R, ...args: TArgs) => R;Captures the current execution context and returns a function that accepts a function as an argument. Whenever the returned function is called, it calls the function passed to it within the captured context.

const asyncLocalStorage = new AsyncLocalStorage(); const runInAsyncScope = asyncLocalStorage.run(123, () => AsyncLocalStorage.snapshot()); const result = asyncLocalStorage.run(321, () => runInAsyncScope(() => asyncLocalStorage.getStore())); console.log(result); // returns 123 AsyncLocalStorage.snapshot() can replace the use of AsyncResource for simple async context tracking purposes, for example:

class Foo { #runInAsyncScope = AsyncLocalStorage.snapshot(); get() { return this.#runInAsyncScope(() => asyncLocalStorage.getStore()); } } const foo = asyncLocalStorage.run(123, () => new Foo()); console.log(asyncLocalStorage.run(321, () => foo.get())); // returns 123 @returnsA new function with the signature (fn: (...args) : R, ...args) : R.

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco

We're hiring →

智能索引记录