Thursday 16 March 2017

how to get return a value from asynchronous function in nodejs?

var fs = require('fs');
var ytdl = require('ytdl-core');

var favicon = require('serve-favicon');
var express = require('express');
var app = express();

app.use(favicon(__dirname + '/public/favicon.png'));
app.get('/:id',function (req,res){
         ID = req.params.id;

var url = 'http://www.youtube.com/watch?v='+ID;

//function
  ytdl(url,
  function(err, format) {

    if (err) throw err;
    
       value =  format.formats;//this is the json objact from ytdl module
       return value;// i want return this value to user
       console.log(format.formats);//i am getting value... in console but not outside of the function..       
});

res.send(ytdl);//i want to send that async objact to this page....

});
app.listen(80);
 

so i want that "value variable" outside of that "async function" and if it's not possible then i want to send that json object to the browser but i don't know how to send that so i am stucked here so please suggest me what should do i next?

ps: i already tried as global variable but still facing problems..

thanks in advance...



via Kartik Garasia

No comments:

Post a Comment