import * as ah from 'async_hooks'; const createStorage = () => { let storage: ah.AsyncLocalStorage; if (typeof ah.AsyncLocalStorage !== 'undefined') { storage = new ah.AsyncLocalStorage(); } const run = (context: T, cb: () => O | Promise): Promise => { if (!storage) { throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17 `); } return new Promise((resolve, reject) => { storage.run(context, () => { try { return resolve(cb()); } catch (error) { return reject(error); } }); }); }; const useContext: () => T = () => { if (!storage) { throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17 `); } const context = storage.getStore(); if (!context) { throw new Error( `Can't call useContext out of scope, it should be placed on top of the function`, ); } return context; }; return { run, useContext, }; }; export { createStorage };