<!DOCTYPE html><htmlclass="default"lang="en"data-base="../"><head><metacharset="utf-8"/><metahttp-equiv="x-ua-compatible"content="IE=edge"/><title>AsyncResource | trilium</title><metaname="description"content="Documentation for trilium"/><metaname="viewport"content="width=device-width, initial-scale=1"/><linkrel="stylesheet"href="../assets/style.css"/><linkrel="stylesheet"href="../assets/highlight.css"/><scriptdefersrc="../assets/main.js"></script><scriptasyncsrc="../assets/icons.js"id="tsd-icons-script"></script><scriptasyncsrc="../assets/search.js"id="tsd-search-script"></script><scriptasyncsrc="../assets/navigation.js"id="tsd-nav-script"></script><scriptasyncsrc="../assets/hierarchy.js"id="tsd-hierarchy-script"></script></head><body><script>document.documentElement.dataset.theme=localStorage.getItem("tsd-theme")||"os";document.body.style.display="none";setTimeout(()=>window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><headerclass="tsd-page-toolbar"><divclass="tsd-toolbar-contents container"><ahref="../index.html"class="title">trilium</a><divid="tsd-toolbar-links"></div><buttonid="tsd-search-trigger"class="tsd-widget"aria-label="Search"><svgwidth="16"height="16"viewBox="0 0 16 16"fill="none"aria-hidden="true"><usehref="../assets/icons.svg#icon-search"></use></svg></button><dialogid="tsd-search"aria-label="Search"><inputrole="combobox"id="tsd-search-input"aria-controls="tsd-search-results"aria-autocomplete="list"aria-expanded="true"autocapitalize="off"autocomplete="off"placeholder="Search the docs"maxLength="100"/><ulrole="listbox"id="tsd-search-results"></ul><divid="tsd-search-status"aria-live="polite"aria-atomic="true"><div>Preparing search index...</div></div></dialog><ahref="#"class="tsd-widget menu"id="tsd-toolbar-menu-trigger"data-toggle="menu"aria-label="Menu"><svgwidth="16"height="16"viewBox="0 0 16 16"fill="none"aria-hidden="true"><usehref="../assets/icons.svg#icon-menu"></use></svg></a></div></header><divclass="container container-main"><divclass="col-content"><divclass="tsd-page-title"><ulclass="tsd-breadcrumb"aria-label="Breadcrumb"><li><ahref="../modules/Backend_Script_API.html">Backend Script API</a></li><li><ahref="../modules/Backend_Script_API._internal_.html"><internal></a></li><li><ahref=""aria-current="page">AsyncResource</a></li></ul><h1>Class AsyncResource</h1></div><sectionclass="tsd-panel tsd-comment"><divclass="tsd-comment tsd-typography"><p>The class <code>AsyncResource</code> is designed to be extended by the embedder's async
resources. Using this, users can easily trigger the lifetime events of their
own resources.</p>
<p>The <code>init</code> hook will trigger when an <code>AsyncResource</code> is instantiated.</p>
<p>The following is an overview of the <code>AsyncResource</code> API.</p>
<pre><codeclass="js"><spanclass="hl-4">import</span><spanclass="hl-1"> { </span><spanclass="hl-5">AsyncResource</span><spanclass="hl-1">, </span><spanclass="hl-5">executionAsyncId</span><spanclass="hl-1"> } </span><spanclass="hl-4">from</span><spanclass="hl-1"></span><spanclass="hl-3">'node:async_hooks'</span><spanclass="hl-1">;</span><br/><br/><spanclass="hl-6">// AsyncResource() is meant to be extended. Instantiating a</span><br/><spanclass="hl-6">// new AsyncResource() also triggers init. If triggerAsyncId is omitted then</span><br/><spanclass="hl-6">// async_hook.executionAsyncId() is used.</span><br/><spanclass="hl-2">const</span><spanclass="hl-1"></span><spanclass="hl-7">asyncResource</span><spanclass="hl-1"> = </span><spanclass="hl-2">new</span><spanclass="hl-1"></span><spanclass="hl-0">AsyncResource</span><spanclass="hl-1">(</span><br/><spanclass="hl-1"></span><spanclass="hl-5">type</span><spanclass="hl-1">, { </span><spanclass="hl-5">triggerAsyncId:</span><spanclass="hl-1"></span><spanclass="hl-0">executionAsyncId</span><spanclass="hl-1">(), </span><spanclass="hl-5">requireManualDestroy:</span><spanclass="hl-1"></span><spanclass="hl-2">false</span><spanclass="hl-1"> },</span><br/><spanclass="hl-1">);</span><br/><br/><spanclass="hl-6">// Run a function in the execution context of the resource. This will</span><br/><spanclass="hl-6">// * establish the context of the resource</span><br/><spanclass="hl-6">// * trigger the AsyncHooks before callbacks</span><br/><spanclass="hl-6">// * call the provided function `fn` with the supplied arguments</span><br/><spanclass="hl-6">// * trigger the AsyncHooks after callbacks</span><br/><spanclass="hl-6">// * restore the original execution context</span><br/><spanclass="hl-5">asyncResource</span><spanclass="hl-1">.</span><spanclass="hl-0">runInAsyncScope</span><spanclass="hl-1">(</span><spanclass="hl-5">fn</span><spanclass="hl-1">, </span><spanclass="hl-5">thisArg</span><spanclass="hl-1">, ...</span><spanclass="hl-5">args</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-6">// Call AsyncHooks destroy callbacks.</span><br/><spanclass="hl-5">asyncResource</span><spanclass="hl-1">.</span><spanclass="hl-0">emitDestroy</span><spanclass="hl-1">();</span><br/><br/><spanclass="hl-6">// Return the unique ID assigned to the AsyncResource instance.</span><br/><spanclass="hl-5">asyncResource</span><spanclass="hl-1">.</span><spanclass="hl-0">asyncId</span><spanclass="hl-1">();</span><br/><br/><spanclass="hl-6">// Return the trigger ID for the AsyncResource instance.</span><br/><spanclass="hl-5">asyncResource</span><spanclass="hl-1">.</span><spanclass="hl-0">triggerAsyncId</span><spanclass="hl-1">();</span>
new AsyncResource() also triggers init. If triggerAsyncId is omitted then
async_hook.executionAsyncId() is used.</p>
</div><divclass="tsd-parameters"><h4class="tsd-parameters-title">Parameters</h4><ulclass="tsd-parameter-list"><li><span><spanclass="tsd-kind-parameter">type</span>: <spanclass="tsd-signature-type">string</span></span><divclass="tsd-comment tsd-typography"><p>The type of async event.</p>
</div><divclass="tsd-comment tsd-typography"></div></li><li><span><codeclass="tsd-tag">Optional</code><spanclass="tsd-kind-parameter">triggerAsyncId</span>: <spanclass="tsd-signature-type">number</span><spanclass="tsd-signature-symbol">|</span><ahref="../interfaces/Backend_Script_API._internal_.AsyncResourceOptions.html"class="tsd-signature-type tsd-kind-interface">AsyncResourceOptions</a></span><divclass="tsd-comment tsd-typography"><p>The ID of the execution context that created
<divclass="tsd-comment tsd-typography"></div><asideclass="tsd-sources"><ul><li>Defined in node_modules/@types/node/async_hooks.d.ts:317</li></ul></aside></div></li></ul></section><sectionclass="tsd-panel tsd-member tsd-is-external"><h3class="tsd-anchor-link"id="bind"><span>bind</span><ahref="#bind"aria-label="Permalink"class="tsd-anchor-icon"><svgviewBox="0 0 24 24"aria-hidden="true"><usehref="../assets/icons.svg#icon-anchor"></use></svg></a></h3><ulclass="tsd-signatures tsd-is-external"><liclass="tsd-is-external"><divclass="tsd-signature tsd-anchor-link"id="bind-1"><spanclass="tsd-kind-call-signature">bind</span><spanclass="tsd-signature-symbol"><</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="#bindfunc">Func</a><spanclass="tsd-signature-keyword">extends</span><spanclass="tsd-signature-symbol">(</span><spanclass="tsd-signature-symbol">...</span><spanclass="tsd-kind-parameter">args</span><spanclass="tsd-signature-symbol">:</span><spanclass="tsd-signature-type">any</span><spanclass="tsd-signature-symbol">[]</span><spanclass="tsd-signature-symbol">)</span><spanclass="tsd-signature-symbol">=></span><spanclass="tsd-signature-type">any</span><spanclass="tsd-signature-symbol">></span><spanclass="tsd-signature-symbol">(</span><spanclass="tsd-kind-parameter">fn</span><spanclass="tsd-signature-symbol">:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="../interfaces/Backend_Script_API._internal_.EventEmitter.EventEmitterReferencingAsyncResource.html#bindfunc">Func</a><spanclass="tsd-signature-symbol">)</span><spanclass="tsd-signature-symbol">:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="../interfaces/Backend_Script_API._internal_.EventEmitter.EventEmitterReferencingAsyncResource.html#bindfunc">Func</a><ahref="#bind-1"aria-label="Permalink"class="tsd-anchor-icon"><svgviewBox="0 0 24 24"aria-hidden="true"><usehref="../assets/icons.svg#icon-anchor"></use></svg></a></div><divclass="tsd-description"><divclass="tsd-comment tsd-typography"><p>Binds the given function to execute to this <code>AsyncResource</code>'s scope.</p>
</div><sectionclass="tsd-panel"><h4>Type Parameters</h4><ulclass="tsd-type-parameter-list"><li><spanid="bindfunc"><spanclass="tsd-kind-type-parameter">Func</span><spanclass="tsd-signature-keyword">extends</span><spanclass="tsd-signature-symbol">(</span><spanclass="tsd-signature-symbol">...</span><spanclass="tsd-kind-parameter">args</span><spanclass="tsd-signature-symbol">:</span><spanclass="tsd-signature-type">any</span><spanclass="tsd-signature-symbol">[]</span><spanclass="tsd-signature-symbol">)</span><spanclass="tsd-signature-symbol">=></span><spanclass="tsd-signature-type">any</span></span></li></ul></section><divclass="tsd-parameters"><h4class="tsd-parameters-title">Parameters</h4><ulclass="tsd-parameter-list"><li><span><spanclass="tsd-kind-parameter">fn</span>: <aclass="tsd-signature-type tsd-kind-type-parameter"href="../interfaces/Backend_Script_API._internal_.EventEmitter.EventEmitterReferencingAsyncResource.html#bindfunc">Func</a></span><divclass="tsd-comment tsd-typography"><p>The function to bind to the current <code>AsyncResource</code>.</p>
</div></div><asideclass="tsd-sources"><ul><li>Defined in node_modules/@types/node/async_hooks.d.ts:290</li></ul></aside></div></li></ul></section><sectionclass="tsd-panel tsd-member tsd-is-external"><h3class="tsd-anchor-link"id="emitdestroy"><span>emit<wbr/>Destroy</span><ahref="#emitdestroy"aria-label="Permalink"class="tsd-anchor-icon"><svgviewBox="0 0 24 24"aria-hidden="true"><usehref="../assets/icons.svg#icon-anchor"></use></svg></a></h3><ulclass="tsd-signatures tsd-is-external"><liclass="tsd-is-external"><divclass="tsd-signature tsd-anchor-link"id="emitdestroy-1"><spanclass="tsd-kind-call-signature">emitDestroy</span><spanclass="tsd-signature-symbol">()</span><spanclass="tsd-signature-symbol">:</span><spanclass="tsd-signature-type">this</span><ahref="#emitdestroy-1"aria-label="Permalink"class="tsd-anchor-icon"><svgviewBox="0 0 24 24"aria-hidden="true"><usehref="../assets/icons.svg#icon-anchor"></use></svg></a></div><divclass="tsd-description"><divclass="tsd-comment tsd-typography"><p>Call all <code>destroy</code> hooks. This should only ever be called once. An error will
be thrown if it is called more than once. This <strong>must</strong> be manually called. If
the resource is left to be collected by the GC then the <code>destroy</code> hooks will
never be called.</p>
</div><h4class="tsd-returns-title">Returns <spanclass="tsd-signature-type">this</span></h4><p>A reference to <code>asyncResource</code>.</p>
<divclass="tsd-comment tsd-typography"></div><asideclass="tsd-sources"><ul><li>Defined in node_modules/@types/node/async_hooks.d.ts:313</li></ul></aside></div></li></ul></section><sectionclass="tsd-panel tsd-member tsd-is-external"><h3class="tsd-anchor-link"id="runinasyncscope"><span>run<wbr/>In<wbr/>Async<wbr/>Scope</span><ahref="#runinasyncscope"aria-label="Permalink"class="tsd-anchor-icon"><svgviewBox="0 0 24 24"aria-hidden="true"><usehref="../assets/icons.svg#icon-anchor"></use></svg></a></h3><ulclass="tsd-signatures tsd-is-external"><liclass="tsd-is-external"><divclass="tsd-signature tsd-anchor-link"id="runinasyncscope-1"><spanclass="tsd-kind-call-signature">runInAsyncScope</span><spanclass="tsd-signature-symbol"><</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="#runinasyncscopethis">This</a><spanclass="tsd-signature-symbol">,</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="#runinasyncscoperesult">Result</a><spanclass="tsd-signature-symbol">></span><spanclass="tsd-signature-symbol">(</span><br/><spanclass="tsd-kind-parameter">fn</span><spanclass="tsd-signature-symbol">:</span><spanclass="tsd-signature-symbol">(</span><spanclass="tsd-kind-parameter">this</span><spanclass="tsd-signature-symbol">:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="../interfaces/Backend_Script_API._internal_.EventEmitter.EventEmitterReferencingAsyncResource.html#runinasyncscopethis">This</a><spanclass="tsd-signature-symbol">,</span><spanclass="tsd-signature-symbol">...</span><spanclass="tsd-kind-parameter">args</span><spanclass="tsd-signature-symbol">:</span><spanclass="tsd-signature-type">any</span><spanclass="tsd-signature-symbol">[]</span><spanclass="tsd-signature-symbol">)</span><spanclass="tsd-signature-symbol">=></span><aclass="tsd-signature-type tsd-kind-type-parameter"href="../interfaces/Backend_Script_API._internal_.EventEmitter.EventEmitterReferencingAsyncResource.html#runinasyncscoperesult">Result</a><spanclass="tsd-signature-symbol">,</span><br/><spanclass="tsd-kind-parameter">thisArg</span><spanclass="tsd-signature-symbol">?:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="../interfaces/Backend_Script_API._internal_.EventEmitter.EventEmitterReferencingAsyncResource.html#runinasyncscopethis">This</a><spanclass="tsd-signature-symbol">,</span><br/><spanclass="tsd-signature-symbol">...</span><spanclass="tsd-kind-parameter">args</span><spanclass="tsd-signature-symbol">:</span><spanclass="tsd-signature-type">any</span><spanclass="tsd-signature-symbol">[]</span><spanclass="tsd-signature-symbol">,</span><br/><spanclass="tsd-signature-symbol">)</span><spanclass="tsd-signature-symbol">:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="../interfaces/Backend_Script_API._internal_.EventEmitter.EventEmitterReferencingAsyncResource.html#runinasyncscoperesult">Result</a><ahref="#runinasyncscope-1"aria-label="Permalink"class="tsd-anchor-icon"><svgviewBox="0 0 24 24"aria-hidden="true"><usehref="../assets/icons.svg#icon-anchor"></use></svg></a></div><divclass="tsd-description"><divclass="tsd-comment tsd-typography"><p>Call the provided function with the provided arguments in the execution context
</div><sectionclass="tsd-panel"><h4>Type Parameters</h4><ulclass="tsd-type-parameter-list"><li><spanid="runinasyncscopethis"><spanclass="tsd-kind-type-parameter">This</span></span></li><li><spanid="runinasyncscoperesult"><spanclass="tsd-kind-type-parameter">Result</span></span></li></ul></section><divclass="tsd-parameters"><h4class="tsd-parameters-title">Parameters</h4><ulclass="tsd-parameter-list"><li><span><spanclass="tsd-kind-parameter">fn</span>: <spanclass="tsd-signature-symbol">(</span><spanclass="tsd-kind-parameter">this</span><spanclass="tsd-signature-symbol">:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="../interfaces/Backend_Script_API._internal_.EventEmitter.EventEmitterReferencingAsyncResource.html#runinasyncscopethis">This</a><spanclass="tsd-signature-symbol">,</span><spanclass="tsd-signature-symbol">...</span><spanclass="tsd-kind-parameter">args</span><spanclass="tsd-signature-symbol">:</span><spanclass="tsd-signature-type">any</span><spanclass="tsd-signature-symbol">[]</span><spanclass="tsd-signature-symbol">)</span><spanclass="tsd-signature-symbol">=></span><aclass="tsd-signature-type tsd-kind-type-parameter"href="../interfaces/Backend_Script_API._internal_.EventEmitter.EventEmitterReferencingAsyncResource.html#runinasyncscoperesult">Result</a></span><divclass="tsd-comment tsd-typography"><p>The function to call in the execution context of this async resource.</p>
</div><divclass="tsd-comment tsd-typography"></div></li><li><span><codeclass="tsd-tag">Optional</code><spanclass="tsd-kind-parameter">thisArg</span>: <aclass="tsd-signature-type tsd-kind-type-parameter"href="../interfaces/Backend_Script_API._internal_.EventEmitter.EventEmitterReferencingAsyncResource.html#runinasyncscopethis">This</a></span><divclass="tsd-comment tsd-typography"><p>The receiver to be used for the function call.</p>
</div><divclass="tsd-comment tsd-typography"></div></li><li><span><spanclass="tsd-signature-symbol">...</span><spanclass="tsd-kind-parameter">args</span>: <spanclass="tsd-signature-type">any</span><spanclass="tsd-signature-symbol">[]</span></span><divclass="tsd-comment tsd-typography"><p>Optional arguments to pass to the function.</p>
</div></div><asideclass="tsd-sources"><ul><li>Defined in node_modules/@types/node/async_hooks.d.ts:301</li></ul></aside></div></li></ul></section><sectionclass="tsd-panel tsd-member tsd-is-external"><h3class="tsd-anchor-link"id="triggerasyncid"><span>trigger<wbr/>Async<wbr/>Id</span><ahref="#triggerasyncid"aria-label="Permalink"class="tsd-anchor-icon"><svgviewBox="0 0 24 24"aria-hidden="true"><usehref="../assets/icons.svg#icon-anchor"></use></svg></a></h3><ulclass="tsd-signatures tsd-is-external"><liclass="tsd-is-external"><divclass="tsd-signature tsd-anchor-link"id="triggerasyncid-1"><spanclass="tsd-kind-call-signature">triggerAsyncId</span><spanclass="tsd-signature-symbol">()</span><spanclass="tsd-signature-symbol">:</span><spanclass="tsd-signature-type">number</span><ahref="#triggerasyncid-1"aria-label="Permalink"class="tsd-anchor-icon"><svgviewBox="0 0 24 24"aria-hidden="true"><usehref="../assets/icons.svg#icon-anchor"></use></svg></a></div><divclass="tsd-description"><h4class="tsd-returns-title">Returns <spanclass="tsd-signature-type">number</span></h4><p>The same <code>triggerAsyncId</code> that is passed to the <code>AsyncResource</code> constructor.</p>
<divclass="tsd-comment tsd-typography"></div><asideclass="tsd-sources"><ul><li>Defined in node_modules/@types/node/async_hooks.d.ts:321</li></ul></aside></div></li></ul></section><sectionclass="tsd-panel tsd-member tsd-is-external"><h3class="tsd-anchor-link"id="bind-2"><codeclass="tsd-tag">Static</code><span>bind</span><ahref="#bind-2"aria-label="Permalink"class="tsd-anchor-icon"><svgviewBox="0 0 24 24"aria-hidden="true"><usehref="../assets/icons.svg#icon-anchor"></use></svg></a></h3><ulclass="tsd-signatures tsd-is-external"><liclass="tsd-is-external"><divclass="tsd-signature tsd-anchor-link"id="bind-3"><spanclass="tsd-kind-call-signature">bind</span><spanclass="tsd-signature-symbol"><</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="#bindfunc-1">Func</a><spanclass="tsd-signature-keyword">extends</span><spanclass="tsd-signature-symbol">(</span><spanclass="tsd-kind-parameter">this</span><spanclass="tsd-signature-symbol">:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="#bindthisarg">ThisArg</a><spanclass="tsd-signature-symbol">,</span><spanclass="tsd-signature-symbol">...</span><spanclass="tsd-kind-parameter">args</span><spanclass="tsd-signature-symbol">:</span><spanclass="tsd-signature-type">any</span><spanclass="tsd-signature-symbol">[]</span><spanclass="tsd-signature-symbol">)</span><spanclass="tsd-signature-symbol">=></span><spanclass="tsd-signature-type">any</span><spanclass="tsd-signature-symbol">,</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="#bindthisarg">ThisArg</a><spanclass="tsd-signature-symbol">></span><spanclass="tsd-signature-symbol">(</span><br/><spanclass="tsd-kind-parameter">fn</span><spanclass="tsd-signature-symbol">:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="#bindfunc-1">Func</a><spanclass="tsd-signature-symbol">,</span><br/><spanclass="tsd-kind-parameter">type</span><spanclass="tsd-signature-symbol">?:</span><spanclass="tsd-signature-type">string</span><spanclass="tsd-signature-symbol">,</span><br/><spanclass="tsd-kind-parameter">thisArg</span><spanclass="tsd-signature-symbol">?:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="#bindthisarg">ThisArg</a><spanclass="tsd-signature-symbol">,</span><br/><spanclass="tsd-signature-symbol">)</span><spanclass="tsd-signature-symbol">:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="#bindfunc-1">Func</a><ahref="#bind-3"aria-label="Permalink"class="tsd-anchor-icon"><svgviewBox="0 0 24 24"aria-hidden="true"><usehref="../assets/icons.svg#icon-anchor"></use></svg></a></div><divclass="tsd-description"><divclass="tsd-comment tsd-typography"><p>Binds the given function to the current execution context.</p>
</div><sectionclass="tsd-panel"><h4>Type Parameters</h4><ulclass="tsd-type-parameter-list"><li><spanid="bindfunc-1"><spanclass="tsd-kind-type-parameter">Func</span><spanclass="tsd-signature-keyword">extends</span><spanclass="tsd-signature-symbol">(</span><spanclass="tsd-kind-parameter">this</span><spanclass="tsd-signature-symbol">:</span><aclass="tsd-signature-type tsd-kind-type-parameter"href="#bindthisarg">ThisArg</a><spanclass="tsd-signature-symbol">,</span><spanclass="tsd-signature-symbol">...</span><spanclass="tsd-kind-parameter">args</span><spanclass="tsd-signature-symbol">:</span><spanclass="tsd-signature-type">any</span><spanclass="tsd-signature-symbol">[]</span><spanclass="tsd-signature-symbol">)</span><spanclass="tsd-signature-symbol">=></span><spanclass="tsd-signature-type">any</span></span></li><li><spanid="bindthisarg"><spanclass="tsd-kind-type-parameter">ThisArg</span></span></li></ul></section><divclass="tsd-parameters"><h4class="tsd-parameters-title">Parameters</h4><ulclass="tsd-parameter-list"><li><span><spanclass="tsd-kind-parameter">fn</span>: <aclass="tsd-signature-type tsd-kind-type-parameter"href="#bindfunc-1">Func</a></span><divclass="tsd-comment tsd-typography"><p>The function to bind to the current execution context.</p>
</div><divclass="tsd-comment tsd-typography"></div></li><li><span><codeclass="tsd-tag">Optional</code><spanclass="tsd-kind-parameter">type</span>: <spanclass="tsd-signature-type">string</span></span><divclass="tsd-comment tsd-typography"><p>An optional name to associate with the underlying <code>AsyncResource</code>.</p>