本文整理匯總了TypeScript中async_hooks.executionAsyncId函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript executionAsyncId函數的具體用法?TypeScript executionAsyncId怎麽用?TypeScript executionAsyncId使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了executionAsyncId函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: setRootContext
setRootContext(key, value) {
log('set:', key, value);
const rootId = async_hooks.executionAsyncId();
if (this.rootMap.has(rootId)) {
const obj = this.rootMap.get(rootId);
this.rootMap.set(rootId, Object.assign({}, obj, {[key]: value}));
} else {
this.rootMap.set(rootId, {[key]: value});
}
log(this.rootMap);
}
示例2: get
/**
* 獲取zonecontext值策略:
* 1. 找出當前async-resource 有context的async-resources集合;
* 2. 按就近原則, 遍曆async-resources集合對應的context;
* 3. 如果context包含key值則返回,如果不包含key值,則繼續;
*
* @param key
* @returns {any}
*/
get(key: string): any | undefined {
let asyncId = async_hooks.executionAsyncId();
let ids = this.getRootAsyncIdLinks(asyncId);
let value = undefined;
for (let i = 0, iLen = ids.length; i < iLen; i++) {
let contextMap = this.rootMap.get(ids[i]);
if (contextMap[key]) {
value = contextMap[key];
break;
}
}
log(`current currentAsyncId ${asyncId} value:${JSON.stringify(value)}`);
return value;
}
示例3: init
////////////////////////////////////////////////////
{
const hooks: async_hooks.HookCallbacks = {
init() {},
before() {},
after() {},
destroy() {},
promiseResolve() {},
};
const asyncHook = async_hooks.createHook(hooks);
asyncHook.enable().disable().enable();
const tId: number = async_hooks.triggerAsyncId();
const eId: number = async_hooks.executionAsyncId();
class TestResource extends async_hooks.AsyncResource {
constructor() {
super('TEST_RESOURCE');
}
}
class AnotherTestResource extends async_hooks.AsyncResource {
constructor() {
super('TEST_RESOURCE', 42);
const aId: number = this.asyncId();
const tId: number = this.triggerAsyncId();
}
run() {
this.runInAsyncScope(() => {});
示例4: function
transport: function (data) {
const asyncId = async_hooks.executionAsyncId()
console.log(asyncId, data.output)
}