Search code examples
javascriptnode.jspromisebluebird

Chain functions using bluebird promises in NodeJS


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.


Solution

  • 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, "<<<<<<<");
        });