javascript - Bluebird.js in Node and asynchronous api calls - Stack Overflow

So I'm trying to build my first webapp with Facebook integration (using facebook-node-sdk). I have

So I'm trying to build my first webapp with Facebook integration (using facebook-node-sdk). I have it making simple calls to the api, but now it's time to put this all in a simple server and make the calls upon request (this isn't going to be the webapp, itself, but more of an API server).

The problem I'm running into is that, even though I've (presumably) used bluebird to Promisify the Facebook sdk and my makeCall method, I'm still getting "hi" printed and then "undefined" - console.log is getting called before makeCall can return anything.

Here's my 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);

So I'm trying to build my first webapp with Facebook integration (using facebook-node-sdk). I have it making simple calls to the api, but now it's time to put this all in a simple server and make the calls upon request (this isn't going to be the webapp, itself, but more of an API server).

The problem I'm running into is that, even though I've (presumably) used bluebird to Promisify the Facebook sdk and my makeCall method, I'm still getting "hi" printed and then "undefined" - console.log is getting called before makeCall can return anything.

Here's my 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);
Share Improve this question edited Jan 18, 2014 at 18:10 Charlie G asked Jan 18, 2014 at 17:55 Charlie GCharlie G 8249 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

new Promise.method doesn't make sense here (or anywhere since it's a function and not a constructor) nor does makeCall.

Try this:

var Promise = require('bluebird')
    , http = require('http')
    , Facebook = require('facebook-node-sdk')
    , config = require('./config')
    , fb = new Facebook({ appId: config.fb.api, secret: config.fb.secret });

Promise.promisifyAll(Facebook.prototype);

http.createServer( function (req, res) {
    fb.apiAsync('/me').then(function (data) {
        console.log(data)
    })
}).listen(8001);

Don't create wrappers when promisifyAll does it for you :)

The problem was that I was neither returning a Promise nor was I resolving said un-returned promise. Here's the fixed code (that works!)

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 new Promise(function (resolve) {
        // resolve
        console.log('resolve')
        fb.api(username, function(err, data) {
            console.log('err: ' + err)
            console.log('data: ' + data)
            if (err) reject(err);

            resolve(data);
        });
    });
});

http.createServer( function (req, res) {
    makeCall('/me').then(function (data) {
        console.log(data)
    })
}).listen(8001);

Where the output looks like:

resolve
err: null
data: [object Object]
{ id: ... }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744316012a4568185.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信