Search code examples
javascriptes6-promise

Ordering promis by execution time


Hello is there any solution to order promises by execution time? for ex

Promise.all([Promise // 5ms ,Promise // 3ms,Promise // 2ms])

will return answer with same order as it was given is there any solution or method to sort it by execution time to have return like?

Promise.all([Promise // 2ms ,Promise // 3ms,Promise // 5ms])

Solution

  • To restate the problem clearly: given a set of promises, can they be executed concurrently, and can the results be arranged in an array in order of execution time?

    If the execution time is known in advance, then sort the promises by execution time and pass the sorted array to Promise.all().

    If the execution times are unknown in advance, I'd suggest wrapping the promises with a little function that times execution. Sort the result of Promise.all() on those elapsed times...

    function sortedPromiseAll(array) {
      const start = new Date()
      const instrumentPromise = p => {
        return p.then(result => {
          const now = new Date();
          return { result, duration: now.getTime()-start.getTime() }
        });
      }
      const instrumentedPromises = array.map(instrumentPromise)
      return Promise.all(instrumentedPromises).then(results => {
        return results.sort((a, b) => a.duration-b.duration).map(r => r.result);
      })
    }
    
    const timedPromise = t => {
      return new Promise(resolve => {
        setTimeout(resolve, t)
      })
    };
    
    // imagine we don't know these timings
    const promiseA = timedPromise(600).then(() => 'A');
    const promiseB = timedPromise(300).then(() => 'B');
    const promiseC = timedPromise(900).then(() => 'C');
    
    // expected result is B, A, C
    sortedPromiseAll([promiseA, promiseB, promiseC])
      .then(result => console.log(result));