henriqueinonhe / promises-training

Practice working with promises through a curated collection of interactive challenges. This repository provides a platform to refine your skills, complete with automated tests to to give you instant feedback and validate your progress.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Improve `promiseAll` and `promiseAllSettled` tests

henriqueinonhe opened this issue · comments

Currently, tests for both promiseAll and promiseAllSettled only check for a specific resolution order for promises and for a single promise list length.

One of the problems that this causes, for example, for the promiseAll exercise is that there is a false negative (test passes but implementation is faulty) for the following implementation:

export default async <T>(promises: Array<Promise<T>>): Promise<Array<T>> => {
  const promiseCount = promises.length;
  const resolvedValues: Array<T> = [];

  return new Promise((resolve, reject) => {
    promises.forEach((promise, index) => {
      promise.then((result) => {
        resolvedValues[index] = result;

        if (resolvedValues.length === promisesCount) {
          resolve(resolvedValues);
        }
      }, reject);
    });
  });
};

In this case, if the last promise is not the last one to be resolved, the outer promise will resolve because when you do array[index] = something, the array is considered to have length === index + 1 for it backfills all elements before the one you're setting with undefined.

Moving forward, we want to do two things:

  1. Create tests for different lengths of promise lists, including an empty list.
  2. For each promise list, we want to exercise all possible orders (permutations) of promise resolution.