简体   繁体   English

Node中的Bluebird.js和异步api调用

[英]Bluebird.js in Node and asynchronous api calls

So I'm trying to build my first webapp with Facebook integration (using facebook-node-sdk ). 所以我正在尝试使用Facebook集成构建我的第一个webapp(使用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). 我让它对api进行简单的调用,但是现在是时候将它们放在一个简单的服务器中并根据请求进行调用(这不是webapp本身,而是更多的API服务器)。

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. 我遇到的问题是,即使我(大概)使用蓝鸟来宣传Facebook sdk和我的makeCall方法,我仍然会打印“hi”,然后“未定义” - console.log正在获取在makeCall之前调用可以返回任何内容。

Here's my app.js : 这是我的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);

new Promise.method doesn't make sense here (or anywhere since it's a function and not a constructor) nor does makeCall . new Promise.method在这里(或任何地方,因为它是一个函数而不是构造函数)没有意义,也没有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 :) 当promisifyAll为你做的时候不要创建包装器:)

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: ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM