انتقل إلى المحتوى

مرجع واجهة ⁦node:async_hooks⁩

توافق جزئي مع ⁦AsyncLocalStorage⁩ و⁦AsyncResource⁩

في هذه الصفحة
Open in ChatGPTOpen in Claude

يوفر ⁦Elide⁩ ‏⁦AsyncLocalStorage⁩ و⁦AsyncResource⁩ و⁦createHook⁩ و⁦executionAsyncId⁩ و⁦triggerAsyncId⁩ و⁦executionAsyncResource⁩ و ⁦asyncWrapProviders⁩.

يدعم ⁦AsyncLocalStorage⁩ التوابع ⁦run⁩ و⁦exit⁩ و⁦getStore⁩ و⁦enterWith⁩ و⁦disable⁩ و⁦snapshot⁩ و⁦bind⁩. وانتشار السياق جزئي: فالشيفرة المتزامنة والمؤقت المُنشأ داخل ⁦run⁩ يمكنهما رؤية المخزن، بينما لا يمكن ذلك لاستكمال ⁦Promise⁩.

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()⁩ كائنًا يتضمن ⁦enable()⁩ و⁦disable()⁩، لكن ⁦Elide⁩ لا يوفر دورة حياة الموارد غير المتزامنة الكاملة في ⁦Node⁩. والمكتبات التي تتطلب السياق عبر ⁦await⁩ لا ينبغي أن تعتمد على هذا التنفيذ.

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.

هل كانت هذه الصفحة مفيدة؟