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

141
Visualizações
how to make wait into a fetch call in javascript

i want to make a breack of 1 sec into a fetch call and it doesn't work, the code is this:

async function sendStats(image, endimage, chat, id) {
   let finalimage = image + endimage;
     fetch(finalimage)
    .then(res => {
        if (res.ok) {
            res.body.pipe(fs.createWriteStream('./imgs/image.png'));
            bot.sendPhoto(chat, finalimage);
        } else {
            finalimage = image + "i" + endimage;
            fetch(finalimage)
                .then(res => {
                    if (res.ok) {
                        res.body.pipe(fs.createWriteStream('./imgs/image.png'));
                        bot.sendPhoto(chat, finalimage);
                    } else {
                        bot.sendMessage(chat, "spiacenti ma non abbiamo un immagine di questo mostro :(");
                    }
                })

        }
        let mstr = mostri.filter(x => x.id == id);
        let desc = mstr[0].description;
        let elements = mstr[0].elements;
        await sleep(1000);
        bot.sendMessage(chat, desc);

    })
    .catch((err) => {
        console.log(err)
    });
}

function sleep(ms) {
return new Promise((resolve) => {
    setTimeout(resolve, ms);
});
}

i want to send the bot.sendmessage of desc after the sendphoto because is reversed now, and it doesn't give me message errors

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue with your code is that you are using the await keyword without an async function.


I think you might've gotten confused with this async function when using the await keyword with sleep().

async function sendStats(image, endimage, chat, id) {}

The issue is that your sleep() function is inside another function, so the sendStats() function won't help with its await keyword.


The solution for this issue is to add an async keyword to the function that the sleep() function is in.

fetch(finalImage)
  .then(async (res) => {
    await sleep(1000);
  });

Line 2 has been modified.

The reason this works is because the function in which the await keyword is in has the async keyword added onto the function, which specifies that the function has an asynchronous task in it.


In conclusion, the issue with your code was that you had forgotten to add the async keyword to the function which had await in there.

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