asi
asi

Reputation: 11

how to run asynchronous function synchronously in node js

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

Upvotes: 0

Views: 176

Answers (1)

Brajendra Swain
Brajendra Swain

Reputation: 355

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);
        }
    });
});

Upvotes: 1

Related Questions