promise-sequential icon indicating copy to clipboard operation
promise-sequential copied to clipboard

Does not work as expected when work with Promise.all

Open Gcaufy opened this issue 3 years ago • 1 comments

Here is the demo code

const sequential = require('promise-sequential');
const array = [1,2,3,4,5];

const makePromise = function(item) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(item)
    }, 1000);
  });
};

const makePromiseAll = function(item) {
  return Promise.all([
    new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('a' + item);
      }, 1000)
    }),
    new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('b' + item);
      }, 1000)
    }),
  ])
}

const makeFunc = function(fn) {
  return function (item) {
    return function(prevResp, resp, count) {
      return fn(item);
    }
  }
};

// Result expected
Promise.all(array.map(item => makePromise(item))).then(res => console.log('promise all result: ', res));
sequential(array.map(item => makeFunc(makePromise)(item))).then(res => console.log('sequential result: ', res));


// Result not expected
Promise.all(array.map(item => makePromiseAll(item))).then(res => console.log('promise all with promise all result: ', res));
sequential(array.map(item => makeFunc(makePromiseAll)(item))).then(res => console.log('sequential with promise all result: ', res));

Here is the result:

promise all result:  [ 1, 2, 3, 4, 5 ]
promise all with promise all result:  [
  [ 'a1', 'b1' ],
  [ 'a2', 'b2' ],
  [ 'a3', 'b3' ],
  [ 'a4', 'b4' ],
  [ 'a5', 'b5' ]
]
sequential result:  [ 1, 2, 3, 4, 5 ]
sequential with promise all result:  [
  'a1', 'b1', 'a2',
  'b2', 'a3', 'b3',
  'a4', 'b4', 'a5',
  'b5'
]

When sequential a simple promise array, it will get the result as expected. But when it work with Promise.all, it make the result array flat.

Is there any way to keep the array?

Gcaufy avatar Mar 10 '21 08:03 Gcaufy

I guess I fixed it by using what I described here in this article I wrote on DEV (I replaced the require with the definition of sequential I gave on the said article but named PromiseExtra.sequence).

const sequential = promises => {
  return promises.reduce((previousPromise, currentPromise) => {
    return previousPromise.then(previousState => {
      return currentPromise(previousState).then(newState => {
        return [
          ...previousState,
          newState
        ];
      });
    });
  }, Promise.resolve([]));
};

const array = [1,2,3,4,5];

const makePromise = function(item) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(item)
    }, 1000);
  });
};

const makePromiseAll = function(item) {
  return Promise.all([
    new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('a' + item);
      }, 1000)
    }),
    new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('b' + item);
      }, 1000)
    }),
  ])
}

const makeFunc = function(fn) {
  return function (item) {
    return function(prevResp, resp, count) {
      return fn(item);
    }
  }
};

// Result expected
Promise.all(array.map(item => makePromise(item))).then(res => console.log('promise all result: ', res));
sequential(array.map(item => makeFunc(makePromise)(item))).then(res => console.log('sequential result: ', res));


// Result not expected
Promise.all(array.map(item => makePromiseAll(item))).then(res => console.log('promise all with promise all result: ', res));
sequential(array.map(item => makeFunc(makePromiseAll)(item))).then(res => console.log('sequential with promise all result: ', res));
promise all result:  [ 1, 2, 3, 4, 5 ]
promise all with promise all result:  [
  [ 'a1', 'b1' ],
  [ 'a2', 'b2' ],
  [ 'a3', 'b3' ],
  [ 'a4', 'b4' ],
  [ 'a5', 'b5' ]
]
sequential result:  [ 1, 2, 3, 4, 5 ]
sequential with promise all result:  [
  [ 'a1', 'b1' ],
  [ 'a2', 'b2' ],
  [ 'a3', 'b3' ],
  [ 'a4', 'b4' ],
  [ 'a5', 'b5' ]
]

I didn't intend to make it a library but if this repository is dead I can make one in a blink, tell me if you are interested.

aminnairi avatar Dec 27 '21 20:12 aminnairi