===== ISTEM =====
Solve the following programming task.

TASK: Custom Promise.all (promiseAll)

Write a function 'promiseAll(promises)' that mimics the behaviour of the native Promise.all. If all promises fulfil it must resolve with an ordered array of results; if any rejects it must reject immediately with that error.

STARTER CODE (javascript):
function promiseAll(promises) {
  // Write your code here
}

module.exports = { promiseAll };


RULES:
- Keep the function name and signature EXACTLY as given.
- Return working code only. No explanations.
- Put the code in a single ``` block.
- Try to solve it first. If you genuinely cannot, write only this single
  line instead of producing faulty code: CANNOT_SOLVE

===== HAM YANIT =====
```javascript
function promiseAll(promises) {
  return new Promise((resolve, reject) => {
    let items;

    try {
      items = Array.from(promises);
    } catch (error) {
      reject(error);
      return;
    }

    const results = new Array(items.length);

    if (items.length === 0) {
      resolve(results);
      return;
    }

    let remaining = items.length;

    items.forEach((promise, index) => {
      Promise.resolve(promise).then(
        (value) => {
          results[index] = value;
          remaining--;

          if (remaining === 0) {
            resolve(results);
          }
        },
        (error) => {
          reject(error);
        }
      );
    });
  });
}

module.exports = { promiseAll };
```