I try to pass parametr to .then in Promise chain. I found this topic Promises, pass additional parameters to then chain but I'm quite new to JS/Promise and cant't match it to my example.
componentDidMount() {
function getData(url) {
return new Promise(function(resolve, reject) {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, true);
xhttp.onload = function() {
if (xhttp.status === 200) {
resolve(JSON.parse(xhttp.response));
} else {
reject(xhttp.statusText);
}
};
xhttp.onerror = function() {
reject(xhttp.statusText);
};
xhttp.send();
});
}
function getHeaders(url) {
return new Promise(function(resolve, reject) {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, true);
xhttp.onload = function() {
if (xhttp.status === 200) {
resolve(getPagesNumber(xhttp.getResponseHeader("Link")));
} else {
reject(xhttp.statusText);
}
};
xhttp.onerror = function() {
reject(xhttp.statusText);
};
xhttp.send();
});
}
var promise = getData("https://api.github.com/orgs/angular/members");
promise.then(function(members) {
console.log(members);
return getHeaders("https://api.github.com/repos/angular/angular-jquery-ui/contributors")
})
.then(function(pgItr) {
console.log(pgItr);
})
.catch(function(error){
console.log(error);
});
}
I need to pass url from
return getHeaders("https://api.github.com/repos/angular/angular-jquery-ui/contributors")
to .then (there where is console.log(pgItr);)
I thought to add a variable just after componentDidMount() { and set this variable (with url) in first .then and than read it in second .then. But I think It's tricky/cheating/non proper solution.