所以我試圖建立我的第一個與Facebook集成的webapp(使用facebook-node-sdk)。我已經對api進行了簡單的調用,但現在是時候把這些全部放在一個簡單的服務器上,並根據請求進行調用(這不是web應用程序本身,而是更多的API服務器)。Bluebird.js在節點和異步API調用
我遇到的問題是,即使我(大概)用於bluebird到Promisify Facebook的SDK和我makeCall
方法,我仍然得到「喜」印刷,然後「未定義」 - console.log
在makeCall可以返回任何東西之前被調用。
這裏是我的app.js
:
var Promise = require('bluebird')
, http = require('http')
, Facebook = Promise.promisifyAll(require('facebook-node-sdk'))
, config = require('./config')
, fb = new Facebook({ appId: config.fb.api, secret: config.fb.secret });
var makeCall = new Promise.method(function (username) {
return fb.api(username, function(err, data) {
console.log('hi')
if (err) return err;
return data;
});
});
http.createServer(function (req, res) {
makeCall('/me').then(console.log)
}).listen(8001);
嗯,'makeCall'是定義服務器創建的外面我的API調用的目的,不要創建包裝稍後可能重用。不過,我承認沒有必要使用新的Promise.method。 –
@charlieg否我的意思是你可以調用'fb.apiAsync'而不是創建包裝 – Esailija
啊好吧。不過,我的問題真的圍繞着使用Promises,而使用解析/拒絕功能的能力對我來說非常重要,在這裏。 –