Promise provides another way to handle events. You can use Promise to organize long sequences of events and simplify code through structured programming.
In some cases, this can make code simpler and clearer, but must be used with caution.
Asynchronous code may be interrupted while waiting, which means things in the world may change outside of your code.
Additionally, errors generated in asynchronous code do not come with stack traces, which makes debugging complex. Consider these factors and use Promise with caution.
Example
// Wait for 2 players to join the world asyncfunctionwaitForPlayers(count) { while (world.querySelectorAll('player').length < count) { const { entity } = awaitworld.nextPlayerJoin(); world.say(entity.player.name + ' has joined'); } }
waitForPlayers(2).then(() =>world.say('The game is ready'));
An optional function to check the event type. If the filter is not true, the event will not be dispatched. If no filter is provided, the future will trigger on the next event.
Typeparam
EventType
Zh
通道发出的事件类型。
En
The event type emitted by the channel.
Zh
一个 Promise,在匹配过滤器的事件触发时解析。
En
A Promise that resolves when an event matching the filter is triggered.
Zh
Promise 提供了另一种处理事件的方式。你可以使用 Promise 来组织长序列的事件,通过结构化编程来简化代码。 在某些情况下,这可以使代码更简单和清晰,但必须谨慎使用。 异步代码在等待时可能会被中断,这意味着世界中的事物可能会在你的代码之外发生变化。 此外,异步代码中生成的错误不会带有堆栈跟踪,这会使调试变得复杂。考虑这些因素并谨慎使用 Promise。
En
Promise provides another way to handle events. You can use Promise to organize long sequences of events and simplify code through structured programming. In some cases, this can make code simpler and clearer, but must be used with caution. Asynchronous code may be interrupted while waiting, which means things in the world may change outside of your code. Additionally, errors generated in asynchronous code do not come with stack traces, which makes debugging complex. Consider these factors and use Promise with caution.
Example
Zh
一个可选的函数,用于检查事件类型。如果过滤器不为真,则事件不会被分发。如果没有提供过滤器,则未来会在下一个事件上触发。
En
An optional function to check the event type. If the filter is not true, the event will not be dispatched. If no filter is provided, the future will trigger on the next event.
Typeparam
EventType
Zh
通道发出的事件类型。
En
The event type emitted by the channel.
Zh
一个 Promise,在匹配过滤器的事件触发时解析。
En
A Promise that resolves when an event matching the filter is triggered.