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

108
Visualizações
Using Promise and await instead of then()

I'm using MusicKit-JS and while this code works I don't know if there's another way of writing it.

The music methods themselves all return __awaiter(...).

Is there a way to write this using promises? I don't know much about them so couldn't get it working using promises.

music.stop()
    .then(function () {
        music.setQueue({})
            .then(function () {
                music.setQueue({ song: id })
                    .then(function () {
                        music.play()
                    })
            })
    });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Assuming these functions are all returning promises, you can wrap it in an async function and then use await and then get rid of the deep nesting:

async function run() {
    await music.stop();
    await music.setQueue({});
    await music.setQueue({song: id});
    await music.play();
}

run().then(() => {
    console.log("done");
}).catch(err => {
    console.log(err);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

All you need to do is declare your functions async. Then you will be able to use await

async function() {
await music.stop();
//    .then(function () {
await        music.setQueue({})
//            .then(function () {
await                music.setQueue({ song: id })
//                    .then(function () {
                        music.play()
                    })
            })
    });
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