0

I am Using Express framework.

The problem I am facing is that I have created a var duration and when a request at '/' is received it simply find the total duration of given video and save to that variable but at the end I want to show that variable but instead of showing that variable value it shows nothing and in console in shows undefined...

can anyone please please help me to tun this code synchronously instead of asynchronous...

thanks alot

This is my code

1 Answer 1

1

to handle the asynchronous code you need to put

console.log(duration);
res.send(duration);

inside the callback of exec.

modified code:

var duration;
app.get('/', function(req, res){
    exec('ffprobe -1 ***', function(){
        if(stdout){
            duration = stdout;
            console.log(duration);
            res.send(duration);
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

This solves the problem, but it's worth mentioning that it doesn't "make the code synchronous". It fixes the previously incorrect asynchronous handling.
thanks for the correction @Robbie - yes!! the code is still asynchronous, now it is just handled correctly. (edited the answer)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.