Sets or gets the default captureRejection value for all emitters.设置或获取所有发射器的默认captureRejection值。
This 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'
侦听器,进程仍将崩溃。
Add a cursor flag to the cursor向游标添加游标标志
The flag to set, must be one of following ['tailable', 'oplogReplay', 'noCursorTimeout', 'awaitData', 'partial' -.要设置的标志必须是以下标志之一:['tailable'
、'oplogReplay'、'noCursorTimeout'
、'awaitData'
、'partial'
-。
The flag boolean value.标志布尔值。
Set the batch size for the cursor.设置游标的批次大小。
The number of documents to return per batch. 每批要返回的文档数。See find command documentation.请参见查找命令文档。
Returns current buffered documents length返回当前缓冲的文档长度
Returns a new uninitialized copy of this cursor, with options matching those that have been set on the current instance返回此游标的新的未初始化副本,其选项与当前实例上设置的选项相匹配
Execute the explain for the cursor执行游标的解释
Iterates over all the documents for this cursor using the iterator, callback pattern.使用迭代器回调模式迭代此游标的所有文档。
If the iterator returns 如果迭代器返回false
, iteration will stop.false
,迭代将停止。
The iteration callback.迭代回调。
Add a group stage to the aggregation pipeline将组阶段添加到聚合管道
Add a limit stage to the aggregation pipeline向聚合管道添加限制阶段
Add a lookup stage to the aggregation pipeline将查找阶段添加到聚合管道
Map all documents using the provided function If there is a transform set on the cursor, that will be called first and the result passed to this function's transform.使用提供的函数映射所有文档如果游标上有一个转换集,则首先调用该转换集,并将结果传递给该函数的转换。
Add a match stage to the aggregation pipeline将匹配阶段添加到聚合管道
Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher)在游标查询上设置maxTimeMS,允许查询的硬超时限制(仅在MongoDB 2.6或更高版本上支持)
Number of milliseconds to wait before aborting the query.中止查询之前等待的毫秒数。
Get the next available document from the cursor, returns null if no more documents are available.从游标获取下一个可用文档,如果没有更多可用文档,则返回null
。
Add an out stage to the aggregation pipeline将输出阶段添加到聚合管道
Add a project stage to the aggregation pipeline将投影阶段添加到聚合管道
Returns current buffered documents返回当前缓冲的文档
Add a redact stage to the aggregation pipeline将编辑阶段添加到聚合管道
Rewind this cursor to its uninitialized state. 将此游标回放到其未初始化状态。Any options that are present on the cursor will remain in effect. 游标上的任何选项都将保持有效。Iterating this cursor will cause new queries to be sent to the server, even if the resultant data has already been retrieved by this cursor.迭代此游标将导致向服务器发送新的查询,即使此游标已经检索到结果数据。
Add a skip stage to the aggregation pipeline向聚合管道添加跳过阶段
Add a sort stage to the aggregation pipeline向聚合管道添加排序阶段
Returns an array of documents. 返回文档数组。The caller is responsible for making sure that there is enough memory to store the results. 调用者负责确保有足够的内存来存储结果。Note that the array only contains partial results when this cursor had been previously accessed. 请注意,当该游标以前被访问时,数组只包含部分结果。In that case, cursor.rewind() can be used to reset the cursor.在这种情况下,可以使用cursor.rewind()
重置游标。
Try to get the next available document from the cursor or 尝试从游标获取下一个可用文档,如果返回空批,则为null
if an empty batch is returnednull
Add a unwind stage to the aggregation pipeline向聚合管道添加展开阶段
Set the ReadPreference for the cursor.设置游标的ReadPreference。
Set the ReadPreference for the cursor.设置游标的ReadPreference。
The new read preference for the cursor.游标的新读取首选项。
Returns 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
,这与调用emitter
上的.listeners
完全相同。
For 对于EventTarget
s this is the only way to get the event listeners for the event target. EventTarget
,这是获取事件目标的事件侦听器的唯一方法。This is useful for debugging and diagnostic purposes.这对于调试和诊断非常有用。
const { getEventListeners, EventEmitter } = require('events');
{
const ee = new EventEmitter();
const listener = () => console.log('Events are fun');
ee.on('foo', listener);
getEventListeners(ee, 'foo'); // [listener]
}
{
const et = new EventTarget();
const listener = () => console.log('Events are fun');
et.addEventListener('foo', listener);
getEventListeners(et, 'foo'); // [listener]
}
A class method that returns the number of listeners for the given 返回在给定eventName
registered on the given emitter
.emitter
上注册的给定eventName
的侦听器数量的类方法。
const { EventEmitter, listenerCount } = require('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事件名称
const { on, EventEmitter } = require('events');
(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')) {
// 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
可用于取消等待事件:
const { on, EventEmitter } = require('events');
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正在侦听的事件的名称
that iterates 迭代eventName
events emitted by the emitter
emitter
发出的eventName
事件
Creates a 创建一个Promise
that is fulfilled when the EventEmitter
emits the given event or that is rejected if the EventEmitter
emits 'error'
while waiting. Promise
,当EventEmitter
发出给定事件时,该Promise
将被满足,或者当EventEmitter
在等待时发出'error'
时,该Promise
被拒绝。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平台'error'
event semantics and does not listen to the 'error'
event.EventTarget
接口配合使用,该接口没有特殊的'error'
事件语义,也不会侦听'error'
。
const { once, EventEmitter } = require('events');
async function run() {
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.log('error happened', err);
}
}
run();
The special handling of the 'error'
event is only used when events.once()
is used to wait for another event. 'error'
事件的特殊处理仅在events.once()
用于等待另一个事件时使用。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:events.once()
用于等待'error'
事件本身,则它将被视为任何其他类型的事件,无需特殊处理:
const { EventEmitter, once } = require('events');
const ee = new EventEmitter();
once(ee, 'error')
.then(([err]) => console.log('ok', err.message))
.catch((err) => console.log('error', err.message));
ee.emit('error', new Error('boom'));
// Prints: ok boom
An AbortSignal
can be used to cancel waiting for the event:AbortSignal
可用于取消等待事件:
const { EventEmitter, once } = require('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!
const { setMaxListeners, EventEmitter } = require('events');
const target = new EventTarget();
const emitter = new EventEmitter();
setMaxListeners(5, target, emitter);
A non-negative number. 非负数。The maximum number of listeners per 每个EventTarget
event.EventTarget
事件的最大侦听器数。
Generated using TypeDoc
The AggregationCursor class is an internal class that embodies an aggregation cursor on MongoDB allowing for iteration over the results returned from the underlying query.AggregationCursor
类是一个内部类,它包含MongoDB上的聚合游标,允许对底层查询返回的结果进行迭代。It supports one by one document iteration, conversion to an array or can be iterated as a Node 4.X or higher stream它支持逐个文档迭代,转换为数组,或者可以作为Node 4X或更高的流进行迭代