Get public user data from github using Promises and xmlhttpRequests?









up vote
1
down vote

favorite












I am trying to get user data from github using an url, I am new to dealing with APIs. I tried to follow github-api, but the code there makes little sense to me. I understand the concept of promises, so I tried to couple up this stackoverflow answer with promises and tried to implement it as below. I am working with node.



var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var request = new XMLHttpRequest();

function pr()
return new Promise(function(resolve, reject)
request.open('get', 'https://api.github.com/users/$username')
request.send();
resolve(request.response);
);


var gitpr = pr();
gitpr.then(function()
console.log(request.response);
)


My request.response [[PromiseValue]] is undefined on running the code in node.



Whereas the result in console is correct if I follow this stackoverflow answer(same as above).










share|improve this question



















  • 1




    you didn't define an "onload" event handler function for your XMLHttpRequest...you're not waiting for the request to complete before you resolve the Promise
    – ADyson
    Nov 10 at 22:45














up vote
1
down vote

favorite












I am trying to get user data from github using an url, I am new to dealing with APIs. I tried to follow github-api, but the code there makes little sense to me. I understand the concept of promises, so I tried to couple up this stackoverflow answer with promises and tried to implement it as below. I am working with node.



var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var request = new XMLHttpRequest();

function pr()
return new Promise(function(resolve, reject)
request.open('get', 'https://api.github.com/users/$username')
request.send();
resolve(request.response);
);


var gitpr = pr();
gitpr.then(function()
console.log(request.response);
)


My request.response [[PromiseValue]] is undefined on running the code in node.



Whereas the result in console is correct if I follow this stackoverflow answer(same as above).










share|improve this question



















  • 1




    you didn't define an "onload" event handler function for your XMLHttpRequest...you're not waiting for the request to complete before you resolve the Promise
    – ADyson
    Nov 10 at 22:45












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to get user data from github using an url, I am new to dealing with APIs. I tried to follow github-api, but the code there makes little sense to me. I understand the concept of promises, so I tried to couple up this stackoverflow answer with promises and tried to implement it as below. I am working with node.



var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var request = new XMLHttpRequest();

function pr()
return new Promise(function(resolve, reject)
request.open('get', 'https://api.github.com/users/$username')
request.send();
resolve(request.response);
);


var gitpr = pr();
gitpr.then(function()
console.log(request.response);
)


My request.response [[PromiseValue]] is undefined on running the code in node.



Whereas the result in console is correct if I follow this stackoverflow answer(same as above).










share|improve this question















I am trying to get user data from github using an url, I am new to dealing with APIs. I tried to follow github-api, but the code there makes little sense to me. I understand the concept of promises, so I tried to couple up this stackoverflow answer with promises and tried to implement it as below. I am working with node.



var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var request = new XMLHttpRequest();

function pr()
return new Promise(function(resolve, reject)
request.open('get', 'https://api.github.com/users/$username')
request.send();
resolve(request.response);
);


var gitpr = pr();
gitpr.then(function()
console.log(request.response);
)


My request.response [[PromiseValue]] is undefined on running the code in node.



Whereas the result in console is correct if I follow this stackoverflow answer(same as above).







javascript node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 22:47

























asked Nov 10 at 22:41









HarshvardhanSharma

8010




8010







  • 1




    you didn't define an "onload" event handler function for your XMLHttpRequest...you're not waiting for the request to complete before you resolve the Promise
    – ADyson
    Nov 10 at 22:45












  • 1




    you didn't define an "onload" event handler function for your XMLHttpRequest...you're not waiting for the request to complete before you resolve the Promise
    – ADyson
    Nov 10 at 22:45







1




1




you didn't define an "onload" event handler function for your XMLHttpRequest...you're not waiting for the request to complete before you resolve the Promise
– ADyson
Nov 10 at 22:45




you didn't define an "onload" event handler function for your XMLHttpRequest...you're not waiting for the request to complete before you resolve the Promise
– ADyson
Nov 10 at 22:45












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You are not doing anything to wait for the request to return!



XMLHttpRequest will notify you when the request is complete with onreadystatechange



Below is a pr() function that accomplishes your objectives, assuming "$username" is something meaningful.



Note: I have put the request object into the function for better encapsulation, and am communicating the value of the response to the caller via the resolve:



function pr() 
var request = new XMLHttpRequest();
return new Promise(function(resolve, reject)
request.onreadystatechange = function()
if (request.readyState == 4)
resolve(request.responseText);


request.open('get', 'https://api.github.com/users/$username', true)
request.send();
);



resolve then sends the responseText member of the response to the function that is awaiting the Promise in the value of it's first function:



pr().then(function(val) 
console.log(val)
, function(err)
console.log(err)
)


In the pr() function we are not calling reject() so that err function can't be called, but it would be a good idea to, for example, check for an error status code and call reject on that.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244143%2fget-public-user-data-from-github-using-promises-and-xmlhttprequests%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    You are not doing anything to wait for the request to return!



    XMLHttpRequest will notify you when the request is complete with onreadystatechange



    Below is a pr() function that accomplishes your objectives, assuming "$username" is something meaningful.



    Note: I have put the request object into the function for better encapsulation, and am communicating the value of the response to the caller via the resolve:



    function pr() 
    var request = new XMLHttpRequest();
    return new Promise(function(resolve, reject)
    request.onreadystatechange = function()
    if (request.readyState == 4)
    resolve(request.responseText);


    request.open('get', 'https://api.github.com/users/$username', true)
    request.send();
    );



    resolve then sends the responseText member of the response to the function that is awaiting the Promise in the value of it's first function:



    pr().then(function(val) 
    console.log(val)
    , function(err)
    console.log(err)
    )


    In the pr() function we are not calling reject() so that err function can't be called, but it would be a good idea to, for example, check for an error status code and call reject on that.






    share|improve this answer
























      up vote
      2
      down vote



      accepted










      You are not doing anything to wait for the request to return!



      XMLHttpRequest will notify you when the request is complete with onreadystatechange



      Below is a pr() function that accomplishes your objectives, assuming "$username" is something meaningful.



      Note: I have put the request object into the function for better encapsulation, and am communicating the value of the response to the caller via the resolve:



      function pr() 
      var request = new XMLHttpRequest();
      return new Promise(function(resolve, reject)
      request.onreadystatechange = function()
      if (request.readyState == 4)
      resolve(request.responseText);


      request.open('get', 'https://api.github.com/users/$username', true)
      request.send();
      );



      resolve then sends the responseText member of the response to the function that is awaiting the Promise in the value of it's first function:



      pr().then(function(val) 
      console.log(val)
      , function(err)
      console.log(err)
      )


      In the pr() function we are not calling reject() so that err function can't be called, but it would be a good idea to, for example, check for an error status code and call reject on that.






      share|improve this answer






















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        You are not doing anything to wait for the request to return!



        XMLHttpRequest will notify you when the request is complete with onreadystatechange



        Below is a pr() function that accomplishes your objectives, assuming "$username" is something meaningful.



        Note: I have put the request object into the function for better encapsulation, and am communicating the value of the response to the caller via the resolve:



        function pr() 
        var request = new XMLHttpRequest();
        return new Promise(function(resolve, reject)
        request.onreadystatechange = function()
        if (request.readyState == 4)
        resolve(request.responseText);


        request.open('get', 'https://api.github.com/users/$username', true)
        request.send();
        );



        resolve then sends the responseText member of the response to the function that is awaiting the Promise in the value of it's first function:



        pr().then(function(val) 
        console.log(val)
        , function(err)
        console.log(err)
        )


        In the pr() function we are not calling reject() so that err function can't be called, but it would be a good idea to, for example, check for an error status code and call reject on that.






        share|improve this answer












        You are not doing anything to wait for the request to return!



        XMLHttpRequest will notify you when the request is complete with onreadystatechange



        Below is a pr() function that accomplishes your objectives, assuming "$username" is something meaningful.



        Note: I have put the request object into the function for better encapsulation, and am communicating the value of the response to the caller via the resolve:



        function pr() 
        var request = new XMLHttpRequest();
        return new Promise(function(resolve, reject)
        request.onreadystatechange = function()
        if (request.readyState == 4)
        resolve(request.responseText);


        request.open('get', 'https://api.github.com/users/$username', true)
        request.send();
        );



        resolve then sends the responseText member of the response to the function that is awaiting the Promise in the value of it's first function:



        pr().then(function(val) 
        console.log(val)
        , function(err)
        console.log(err)
        )


        In the pr() function we are not calling reject() so that err function can't be called, but it would be a good idea to, for example, check for an error status code and call reject on that.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 23:10









        bluejack

        198112




        198112



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244143%2fget-public-user-data-from-github-using-promises-and-xmlhttprequests%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto