Zum Inhalt springen

API-Referenz zu node:async_hooks

Teilweise Kompatibilität für AsyncLocalStorage und AsyncResource

Auf dieser Seite
Open in ChatGPTOpen in Claude

Elide stellt AsyncLocalStorage, AsyncResource, createHook, executionAsyncId, triggerAsyncId, executionAsyncResource und asyncWrapProviders bereit.

AsyncLocalStorage unterstützt run, exit, getStore, enterWith, disable, snapshot und bind. Die Kontextweitergabe ist unvollständig: Synchroner Code und ein innerhalb von run erstellter Timer können den Store sehen, eine Promise-Fortsetzung dagegen nicht.

import { AsyncLocalStorage } from "node:async_hooks";

const storage = new AsyncLocalStorage();
storage.run("request", () => {
  console.log(storage.getStore()); // request

  Promise.resolve().then(() => {
    console.log(storage.getStore()); // undefined
  });
});

createHook() gibt ein Objekt mit enable() und disable() zurück, aber Elide stellt nicht den vollständigen Lebenszyklus asynchroner Ressourcen von Node bereit. Bibliotheken, die Kontext über await hinweg benötigen, sollten sich nicht auf diese Implementierung verlassen.

Reference

Status: Partial · 36 of 45 suite tests pass (80.0%) · since 1.1.0

Exports

ExportKindStatusNotes
async_hooksmodulePartialhooks fire for timers/immediates/promises but not yet for TCP/HTTP/TLS native resources
createHook()functionFullCreates a new AsyncHook instance
executionAsyncId()functionFullReturns the asyncId of the current execution context
triggerAsyncId()functionFullReturns the ID of the resource responsible for calling the callback that is currently being executed
AsyncLocalStoragepropertyFullThe AsyncLocalStorage class
AsyncResourcepropertyFullThe AsyncResource class

Tests: node-api node:async-hooks: 36 pass, 8 fail, 1 error, 3 skipped. Failing: test-async-hooks-destroy-on-gc.js, test-async-hooks-enable-recursive.js, test-async-hooks-fatal-error.js, test-async-hooks-http-parser-destroy.js, test-async-hooks-top-level-clearimmediate.js, test-async-local-storage-http-agent.js, test-async-local-storage-http-multiclients.js, test-async-local-storage-http-parser-leak.js, and 1 more.

War diese Seite hilfreich?