Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
Nested async/await usage

Given the example below, if I wanted to await player.play(), do I have to add async in TWO places since foo will ultimately be the executed function?

example:

const foo = (player) => {

    let foo = videojs;

    foo.addEventListener('ready', () => {
       player.play()
    }

}

would it be:

const foo = async (player) => {

    let foo = videojs;

    foo.addEventListener('ready', async () => {
       await player.play()
    }

}

await foo();

or:

const foo = (player) => {

    let foo = videojs;

    foo.addEventListener('ready', async () => {
       await player.play()
    }

}

foo();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want foo to return a promise that resolves when the .play()-returned promise resolves, then you could promisify addEventListener.

Something like this:

const whenEvent = (elem, event) => new Promise((resolve) =>
    elem.addEventListener(event, resolve, { once: true })
);

const foo = async (player) => {
    await whenEvent(videojs, 'ready');
    await player.play();
};

foo().then(() => console.log("foo resolved"));

Note that a promise can only resolve once, while an event listener can be called multiple times. So this promisified version (whenEvent) will only capture the first event, and then stop listening. whenEvent (and thus foo) should be called again to deal with a next occurrence of the event.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda