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
JavaScript fetch is delayed

I have an Express server waiting for my website to do something. When my site does something, a shell script should be called on the Express server. The problem is: The shell script is only run after the "confirm window" has been accepted or denied. I want the fetch to happen as soon as possible. I wouldn't even need to get anything from the Express server, I just want to signal Express to run the shell script as soon as possible.

I have this code on the website:

messaging.onMessage(function (payload){

    fetch("http://localhost:9000/testAPI")
        .then(res => res.text())
        .then(res => console.log("something:" + res));


    var r = confirm(callingname + " is calling.");
    if (r == true) {
        window.open(payload.data.contact_link, "_self");
    } else {
        console.log("didn't open");
    }
});

I have this code on the backend:

var express = require("express");
var router = express.Router();

router.get("/", function(req,res,next){
    const { exec } = require('child_process');
    exec('bash hi.sh',
        (error, stdout, stderr) => {
            console.log(stdout);
            console.log(stderr);
            if (error !== null) {
                console.log(`exec error: ${error}`);
            }
        });
    res.send("API is working");
});

module.exports = router;
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

confirm() is blocking, and you only have a single thread. This means confirm() will stop the world for your application, preventing fetch() from doing anything.

As the simplest possible fix, you can try delaying the moment when confirm() is invoked. This would allow fetch() to get the request out.

messaging.onMessage(function (payload) {
    fetch("http://localhost:9000/testAPI")
        .then(res => res.text())
        .then(text => console.log("something:" + text));
    
    setTimeout(function () {
        if (confirm(`${callingname} is calling.`)) {
            window.open(payload.data.contact_link, "_self");
        } else {
            console.log("didnt open");
        }
    }, 50);
});

Other options would be to put confirm() into one of the .then() callbacks of fetch, or to use a non-blocking alternative to confirm(), as suggested in the comments.

about 4 years ago · Santiago Trujillo 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