Thursday, 27 April 2017

When running node in shell, can you specifically set a variable equal to a promises resolve return?

I am just getting the feel for the facebook api and am trying to stage by stage play around with the requests.

At first I set a var groupId = "string", then use that groupId in a function I wrote that goes like this

function getPost(groupId){
    return new Promise(function(resolve, reject){
        fb.api(groupId+'/feed?limit=1', function(post){
        resolve(post.data[0]) // post.data[0] being the actual post object
        })
    })
 }

and set var post = getPost(groupId) this sets post equal to a promise which when I type into the node interpreter

post

outputs

Promise {
    { message: 'Who invented gravity?',
      updated_time: '2017-04-27T14:43:38+0000',
      id: '154420241260828_1383910878311752' }
}

To access or use any data from this variable I have to type post.then(function(variable){console.log("now I can use the variable as a variable "+variable)}) But what I want to know is how can I actually set a global variable, or just variable in my node.js intepreter command line to something from the promise? ie. not have to use .then because I am doing this very step by step through command line so I want to store these variables as variables which I can use.

I hope you understand what I mean?

I've tried going var string = post.then(function(data){return data}) but this just returns a promise too!!!

Node.js in the terminal https://gyazo.com/ee207e758adeb05a25f82557f9ce964f

Thank you :)



via lopu

No comments:

Post a Comment