it is required to execute several sql queries to MySQL

I implement the command call through Promise

but the question arose how to make a universal method of processing a sequence of queries sequentially?

more specifically on Promise to implement such a call chain?

class myClass() { constructor() { this.list = [sq1,sql2,sql3]; } function myFunc(i) { ... if(++i < list.length) { if(...) list.push(newSQL); <= {1} <= this.myFunc(i) } } } let m = new MyClass(); m.myFunct(0); 

UPDATE

Now, one iteration is implemented, the question is how to consistently process the entire list:

  myBuy(sql) { return new Promise((resolve, reject) => { ... let promise = global._query(sql); promise .then( data=>resolve(data), err=>reject(err) ) }) } listSQL.forEach((e) => myClass.myBuy(e) ) 
  • Do you want promises to be performed one by one in sequence? parallel? in case of failure, what will happen to the others? - Mikhail Vaysman
  • @MikhailVaysman, consistently. Failure in this task is not critical. Question how to Promise.all make a sequential and processing dynamic (padded) array. - ravend
  • supplemented at what moment? - Mikhail Vaysman
  • added code, see the line with {1} . - ravend
  • @MikhailVaysman, although the dynamism is not critical. More question in последовательности - ravend

1 answer 1

Something like this may be written, but I would recommend using ready-made libraries.
I used to use async for such things.

 let list = [ Promise.resolve(1), Promise.resolve(2), ]; const add = (p, newP) => p.then(results => newP.then(newRes => { results.push(newRes); return results; })); let p = Promise.resolve([]); list.forEach(newP => p = add(p, newP)); p = add(p, Promise.resolve(3)); p = add(p, Promise.resolve(4)); p.then(results => console.log(results)); p = add(p, Promise.resolve(5)); p.then(results => console.log(results));