0
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){
\t 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);
,所以我想這是「異步函數」的「值變量」外,如果它是不可能的,那麼我想是JSON對象發送到瀏覽器,但我不知道如何發送,所以我被困在這裏,所以請建議我接下來應該做什麼?
PS:我已經嘗試過作爲全局變量,但仍然面臨的問題..
在此先感謝...
您無法從異步JavaScript函數返回值。任務完成後傳回信息的唯一方法是通過回調。我看到你正在使用其中的一些,但你可能不知道如何或爲什麼需要它們。請研究回調,有很多可用的信息,這個問題已經被回答了很多次。 – magreenberg