Javascript - How to use promises with multiple .then ()

I am trying to create an array structure. So we have parts, and each part contains articles.

So, I promised to collect all the parts. .then()

I need to repeat a partial promise and select the articles in this part .then()

. I want to push this to parts of the array and do it in the view.

The structure is as follows:

-PARTS
      - part
            - u_order
            - u_familia
            - u_part
            - u_type
            - articles (article from each part)

      

And my code:

var p1 = new Promise(function(resolve, reject) {
      var stamp = req.params.stamp;
      request.query("SELECT u_order, u_familia, u_part, u_type FROM u_part (nolock) where u_order <'92' and u_order <> '100'").then((data)=>resolve(data));
      // or
      // reject ("Error!");
    });



    p1.then(function(value){
      var stamp = req.params.stamp;
      console.log(value.length);
      for(var i= 0; i<value.length; i++)
      {
        console.log(value[i]);
        request.query("SELECT st.u_posic, sc.ref, sc.qtt, sc.design FROM st INNER JOIN sc ON st.ref = sc.ref where sc.ststamp ='"+stamp+"' and st.u_posic = '"+value[i].u_order+"'").then((data)=>resolve(data));
      }

    }, function(reason){
      console.log(reason);
    });

    p1.then(function(part, articles){
      var parts = [];
      console.log("PART: " +part.length);
      for(var j= 0; j<part.length; j++)
      {
        console.log(part[j].u_order);
        console.log(part[j].u_familia);
        console.log(part[j].u_part);
        console.log(part[j].u_type);
        console.log(articles[j]);
      };
    });

      

In the latter .then()

, I just have the parts, I cannot access the articles, perhaps because I didn’t do well with the second .then()

I am getting started with promises, I also read the documentation, but I cannot do it.

Can anyone help me understand and solve this problem?

thank

+3


source to share


2 answers


Best I can do - I'll explain a little later (sorry, gotta go)

request.query("SELECT u_order, u_familia, u_part, u_type FROM u_part (nolock) where u_order <'92' and u_order <> '100'")
.then(function(parts){
    var stamp = req.params.stamp;
    return Promise.all(parts.map(function(part) {
        return request.query("SELECT st.u_posic, sc.ref, sc.qtt, sc.design FROM st INNER JOIN sc ON st.ref = sc.ref where sc.ststamp ='"+stamp+"' and st.u_posic = '"+part.u_order+"'")
        .then(function(articles) {
            part.articles = articles;
            return part;
        });
    }));
})
.then(function(parts){
    parts.forEach(function(part) {
        console.log(part.u_order);
        console.log(part.u_familia);
        console.log(part.u_part);
        console.log(part.u_type);
        part.articles.forEach(function(article) {
            console.log(article.u_posic);
            console.log(article.ref);
            console.log(article.qtt);
            console.log(article.design);
        });
    });
});

      



BONUS ES2015 + version above

request.query("SELECT u_order, u_familia, u_part, u_type FROM u_part (nolock) where u_order <'92' and u_order <> '100'")
.then(parts => Promise.all(parts.map(part => request.query(`SELECT st.u_posic, sc.ref, sc.qtt, sc.design FROM st INNER JOIN sc ON st.ref = sc.ref where sc.ststamp ='${req.params.stamp}' and st.u_posic = '${part.u_order}'`)
    .then(articles => {
        part.articles = articles;
        return part;
    })
)))
.then(parts => parts.forEach(part => {
    console.log(part.u_order);
    console.log(part.u_familia);
    console.log(part.u_part);
    console.log(part.u_type);
    part.articles.forEach(article => {
        console.log(article.u_posic);
        console.log(article.ref);
        console.log(article.qtt);
        console.log(article.design);
    });
}));

      

+3


source


You are mistaken.

  • First promise. prototype.then only allow one argument. Check docs

  • And the second promise returns a promise, so you just call the request only there, so no changes in the first allowed data and in its chain just don't pass the first data, since this

  • You can do it in two ways
    • First, by taking a global variable:
const $Vals = {};
function A() {
  return new Promise((resolve, reject) => {
    resolve([1, 2, 3])
  })
}

function B() {
  return new Promise((resolve, reject) => {
    resolve([4, 5, 6])
  })
}



A().then((dt) => {
  $Vals.A = dt;
  return B();
}).then((dt) => {
  $Vals.B = dt;
  console.log('%j', $Vals);
})

      



: {"A": [1,2,3], "B": [4,5,6]}

  • Secondly, it is more convenient if you fulfill an individual promise:
A().then((dt) => new Promise((resolve) => {
  B().then((bData) => {
    resolve({
      A:dt,
      B:bData,
    })
  })
})).then((combined) => {
  console.log('%j', combined);
})

Output: {"A":[1,2,3],"B":[4,5,6]}

      

+1


source







All Articles