x = 0;
T.get('statuses/user_timeline', function(err, data, response) {
x = data[0].text
});
console.log(x);
我使用twit來訪問Twitter的API節點,並且無法弄清楚如何將數據從API分配給公共變量。其github頁面上的所有示例均使用此格式。例如,我怎樣才能在這裏獲取console.log來打印最新的tweet,而不是0?如何從這個內聯函數的範圍中獲取這些數據?
Twit本身的一切工作正常。我只是有這個JS問題。
可能重複[如何從Ajax調用返回響應?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – 2014-11-22 09:35:21
基本上,你沒有。您必須將代碼構建爲異步工作。這需要精神轉變。 – 2014-11-22 09:35:53