Optional
clientOptional
clusterOptional
operationStatic
Readonly
captureStatic
captureSets or gets the default captureRejection value for all emitters.设置或获取所有发射器的默认captureRejection
值。
Static
defaultStatic
Readonly
errorThis symbol shall be used to install a listener for only monitoring 此符号应用于安装仅用于监视'error'
events. 'error'
事件的侦听器。Listeners installed using this symbol are called before the regular 在调用常规'error'
listeners are called.'error'
侦听器之前,将调用使用此符号安装的侦听器。
Installing a listener using this symbol does not change the behavior once an 一旦发出'error'
event is emitted, therefore the process will still crash if no regular 'error'
listener is installed.'error'
事件,使用此符号安装侦听器不会改变行为,因此,如果未安装常规的'error'
侦听器,进程仍将崩溃。
The server id associated with this session与此会话关联的服务器id
Whether or not this session is configured for snapshot reads是否为此会话配置快照读取
Advances the clusterTime for a ClientSession to the provided clusterTime of another ClientSession将ClientSession
的clusterTime
提前到另一个ClientSession
提供的clusterTime
the $clusterTime returned by the server from another session in the form of a document containing the 服务器以包含BSON.Timestamp
clusterTime and signatureBSON.Timestamp
clusterTime和签名的文档形式从另一个会话返回的$clusterTime
Advances the operationTime for a ClientSession.ClientSession的提前操作时间。
the 希望前进到的操作类型的BSON.Timestamp
of the operation type it is desired to advance toBSON.Timestamp
Rest
...args: Parameters<ClientSessionEvents[EventKey]>Ends this session on the server在服务器上结束此会话
Optional
options: EndSessionOptionsOptional settings. Currently reserved for future use可选设置。当前保留以备将来使用
Used to determine if this session equals another用于确定此会话是否等于另一个会话
The session to compare to要比较的会话
Optional
event: string | symbol | EventKeyStarts a new transaction with the given options.使用给定选项启动新事务。
Optional
options: TransactionOptionsOptions for the transaction事务选项
Starts a transaction and runs a provided function, ensuring the commitTransaction is always attempted when all operations run in the function have completed.启动事务并运行提供的函数,确保在函数中运行的所有操作完成后始终尝试commitTransaction
。
IMPORTANT: This method requires the user to return a Promise, and 此方法要求用户返回Promise,并await
all operations.await
所有操作。
callback to run within a transaction在事务中运行的回调
Optional
options: TransactionOptionsoptional settings for the transaction事务的可选设置
A raw command response or undefined原始命令响应或未定义
This function:此功能:
commitTransaction
operation is successful, then this function will return the result of the provided function.commitTransaction
操作成功,则此函数将返回所提供函数的结果。Checkout a descriptive example here:在此处签出一个描述性示例:
https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-implement-transactions
Static
getReturns a copy of the array of listeners for the event named 返回名为eventName
.eventName
的事件的侦听器数组的副本。
For 对于EventEmitter
s this behaves exactly the same as calling .listeners
on the emitter.EventEmitter
,其行为与在发射器上调用.listeners
完全相同。
For 对于EventTarget
s this is the only way to get the event listeners for the event target. This is useful for debugging and diagnostic purposes.EventTarget
,这是获取事件目标的事件侦听器的唯一方法。这对于调试和诊断非常有用。
import { getEventListeners, EventEmitter } from 'node:events';
{
const ee = new EventEmitter();
const listener = () => console.log('Events are fun');
ee.on('foo', listener);
console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
}
{
const et = new EventTarget();
const listener = () => console.log('Events are fun');
et.addEventListener('foo', listener);
console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
}
v15.2.0, v14.17.0
Static
listenerA class method that returns the number of listeners for the given 一个类方法,用于返回在给定eventName
registered on the given emitter
.emitter
上注册的给定eventName
的侦听器数。
import { EventEmitter, listenerCount } from 'node:events';
const myEmitter = new EventEmitter();
myEmitter.on('event', () => {});
myEmitter.on('event', () => {});
console.log(listenerCount(myEmitter, 'event'));
// Prints: 2
The emitter to query要查询的发射器
The event name事件名称
v0.9.12
Since v3.2.0 - Use listenerCount
instead.
Static
onimport { on, EventEmitter } from 'node:events';
import process from 'node:process';
const ee = new EventEmitter();
// Emit later on
process.nextTick(() => {
ee.emit('foo', 'bar');
ee.emit('foo', 42);
});
for await (const event of on(ee, 'foo')) {
// The execution of this inner block is synchronous and it
// processes one event at a time (even with await). Do not use
// if concurrent execution is required.
console.log(event); // prints ['bar'] [42]
}
// Unreachable here
Returns an 返回一个迭代AsyncIterator
that iterates eventName
events. eventName
事件的AsyncIterator
。It will throw if the 如果EventEmitter
emits 'error'
. EventEmitter
发出'error'
,它将抛出。It removes all listeners when exiting the loop. The 它在退出循环时删除所有侦听器。每次迭代返回的value
returned by each iteration is an array composed of the emitted event arguments.value
是一个由发出的事件参数组成的数组。
An AbortSignal
can be used to cancel waiting on events:AbortSignal
可用于取消等待事件:
import { on, EventEmitter } from 'node:events';
import process from 'node:process';
const ac = new AbortController();
(async () => {
const ee = new EventEmitter();
// Emit later on
process.nextTick(() => {
ee.emit('foo', 'bar');
ee.emit('foo', 42);
});
for await (const event of on(ee, 'foo', { signal: ac.signal })) {
// The execution of this inner block is synchronous and it
// processes one event at a time (even with await). Do not use
// if concurrent execution is required.
console.log(event); // prints ['bar'] [42]
}
// Unreachable here
})();
process.nextTick(() => ac.abort());
The name of the event being listened for正在侦听的事件的名称
Optional
options: StaticEventEmitterOptionsthat iterates eventName
events emitted by the emitter
v13.6.0, v12.16.0
Static
onceCreates a 创建一个Promise
that is fulfilled when the EventEmitter
emits the given event or that is rejected if the EventEmitter
emits 'error'
while waiting.Promise
,该Promise在EventEmitter
发出给定事件时实现,或者如果EventEmitter
在等待时发出'error'
则被拒绝。
The Promise
will resolve with an array of all the arguments emitted to the given event.Promise
将通过向给定事件发出的所有参数的数组进行解析。
This method is intentionally generic and works with the web platform EventTarget interface, which has no special此方法是有意通用的,可与web平台EventTarget接口配合使用,该接口没有特殊的'error'
event semantics and does not listen to the 'error'
event.'error'
事件语义,也不侦听'error'
消息。
import { once, EventEmitter } from 'node:events';
import process from 'node:process';
const ee = new EventEmitter();
process.nextTick(() => {
ee.emit('myevent', 42);
});
const [value] = await once(ee, 'myevent');
console.log(value);
const err = new Error('kaboom');
process.nextTick(() => {
ee.emit('error', err);
});
try {
await once(ee, 'myevent');
} catch (err) {
console.error('error happened', err);
}
The special handling of the 'error'
event is only used when events.once()
is used to wait for another event. If events.once()
is used to wait for the
'error'
event itself, then it is treated as any other kind of event without
special handling:
import { EventEmitter, once } from 'node:events';
const ee = new EventEmitter();
once(ee, 'error')
.then(([err]) => console.log('ok', err.message))
.catch((err) => console.error('error', err.message));
ee.emit('error', new Error('boom'));
// Prints: ok boom
An AbortSignal
can be used to cancel waiting for the event:
import { EventEmitter, once } from 'node:events';
const ee = new EventEmitter();
const ac = new AbortController();
async function foo(emitter, event, signal) {
try {
await once(emitter, event, { signal });
console.log('event emitted!');
} catch (error) {
if (error.name === 'AbortError') {
console.error('Waiting for the event was canceled!');
} else {
console.error('There was an error', error.message);
}
}
}
foo(ee, 'foo', ac.signal);
ac.abort(); // Abort waiting for the event
ee.emit('foo'); // Prints: Waiting for the event was canceled!
Optional
options: StaticEventEmitterOptionsv11.13.0, v10.16.0
Optional
options: StaticEventEmitterOptionsStatic
setimport { setMaxListeners, EventEmitter } from 'node:events';
const target = new EventTarget();
const emitter = new EventEmitter();
setMaxListeners(5, target, emitter);
Optional
n: numberA non-negative number. The maximum number of listeners per 一个非负数。每个EventTarget
event.EventTarget
事件的最大侦听器数。
Rest
...eventTargets: (EventEmitter | _DOMEventTarget)[]v15.4.0
Generated using TypeDoc
A class representing a client session on the server表示服务器上客户端会话的类NOTE: not meant to be instantiated directly.注意:不意味着直接实例化。