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).
javascript node.js
add a comment |
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).
javascript node.js
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
add a comment |
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).
javascript node.js
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
javascript node.js
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 10 at 23:10
bluejack
198112
198112
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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