0

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.

NetPax
  • 111
  • 1
  • 6
  • The string `"https://api.github.com/repos/angular/angular-jquery-ui/contributors"` appears to be a constant. Why do you need to pass it around? Why not just declare a constant variable in the same scope as your `var promise`? – Bergi Dec 23 '17 at 20:12
  • Because url will be generated dynamicly, then I get how much subpages It has and pass this number to next .then but I need have a url too: url-subpages relation. – NetPax Dec 23 '17 at 20:28
  • 1
    Well that's not what happened in the code you posted. Please [edit] your question – Bergi Dec 23 '17 at 20:30
  • It isn't entirely clear what you're asking for help with, but perhaps one of these techniques will help: [How to chain and share prior results with Promises](https://stackoverflow.com/questions/28714298/how-to-chain-and-share-prior-results-with-promises/28714863#28714863). – jfriend00 Dec 24 '17 at 05:03
  • Hi, I'm new to Stack and I have a question: I putted a wrong code to the question. What should I do: a) delete question and ask It again or b) change code in this question or c) add new similar question? – NetPax Dec 26 '17 at 10:14

1 Answers1

0

You can always use Promise.all to return more then one promise or value from then like this:

const somePromise = Promise.resolve("some value");
somePromise
    .then(someValue => Promise.all([someValue, Promise.resolve("other value")]))
    .then(([someValue, otherValue]) => console.log(`[${someValue}] and [${otherValue}]`));
Pavel Agarkov
  • 3,633
  • 22
  • 18