What you can do is assign the listener of the second event only when the first event occurs; and, when executed, remove the listener, so that it is added again in the execution of the first event. The logic would look like this:
function eventoX(event) {
// ...
document.removeEventListener('X', eventoX);
}
document.addEventListener('Y', event => {
// ...
document.addEventListener('X', eventoX);
});
For example, considering that both events are button clicks, we will make the click event on button B only occur if button A has already been pressed:
const A = document.getElementById('a');
const B = document.getElementById('b');
function eventoEmB(event) {
console.log('Você pressionou o botão B');
B.removeEventListener('click', eventoEmB);
}
A.addEventListener('click', event => {
console.log('Você pressionou o botão A');
B.addEventListener('click', eventoEmB);
});
<button id="a">Botão A</button>
<button id="b">Botão B</button>
Notice that the event in B will only be triggered if the event in A has been triggered only once.