Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!