0

I'm sorry for asking yet another Promise question however, I can't quite grasp it and after reading tons of questions on here and explanations I'm still struggling.

So in my nodejs project, I'm trying to do three things.

1) Get user info from the Facebook API

graph.get(message.user, function getUserInfo(err, res) {
     console.log(res)
}

2) Get a list of users from another API

request.get('https://api-url/api/users', {
        'auth': {
            'bearer': 'bearerAuth'
        }
    })

3) Check the name from the Facebook user matches a name in the JSON data I get back from my API then hand it to the user.

let aPieceOfData = "";

Bluebird.promisifyAll(graph.get(message.user))
    .then(function(res) {
        // this should give me the response from the Facebook API which is the user
        // Then pass the response to the next .then(function(){})
    })
    .then(function(res) {
       request.get('https://api-url/api/users', {
         'auth': {
           'bearer': 'bearerAuth'
         }
         const apiData = JSON.parse(response.body);
         for (i in apiData) {
          if (res.username == apiData[i].username) {
            // if the username matches the user name then save some data to a variable outside this scope so I can access it
            aPieceOfData = apiData[i].item;
          }
         }
       })
    })
    .catch(function(err) {
        console.log(err, "<<<<<<<");
    })

Formatting might be a little off. But I'm struggling to understand how promises work and how I can pass the data between my chained functions than at the end save it outside my function so I can use it.

Can someone give a bit of explanation and/or some links to beginner friendlier explanations.

1
  • try like that var g = Bluebird.promisifyAll(graph); g.get(message.user).then(...) Commented Jan 20, 2017 at 15:46

1 Answer 1

1

Based on the example from the doc

var fs = Promise.promisifyAll(require("fs"));

fs.readFileAsync("myfile.js", "utf8").then(function(contents) {
    console.log(contents); }).catch(function(e) {
    console.error(e.stack); });

I believe it should be like this:

var g = Bluebird.promisifyAll(graph);
g.getAsync(message.user)
    .then(function (res) {
        // this should give me the response from the Facebook API which is the user
        // Then pass the response to the next .then(function(){})
        return res;
    })
    .then(function (res) {
        return request.get('https://api-url/api/users', {
            'auth': {
                'bearer': 'bearerAuth'
            }
        });
    })
    .then(function (response) {
        const apiData = JSON.parse(response.body);
        for (i in apiData) {
            if (res.username == apiData[i].username) {
                // if the username matches the user name then save some data to a variable outside this scope so I can access it
                aPieceOfData = apiData[i].item;
            }
        }
    })
    .catch(function (err) {
        console.log(err, "<<<<<<<");
    });
Sign up to request clarification or add additional context in comments.

9 Comments

I get a TypeError: g.get(...).then is not a function I have included Bluebird above though like such... const Bluebird = require("bluebird")
can you setup a plunker?
Nope :) I've been doing a bit of reading on promises to try and figure out what i'm missing. So the return statement on the request.get method is returning a promise not the value
So in my third .then i'm getting the value from res instead of from response
@SmurfEkko, sure, if it helped, you can upvote or accept the answer :). You can also update your question with details regarding So in my third .then i'm getting the value from res instead of from response
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.