本文整理汇总了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)
}