JavaScript Lab Sandbox

JavaScript Interactive Playground

Write, execute, and debug polyfills and algorithms in real time with VS Code's Monaco engine.

Load Snippet:
JavaScript Lab Sandbox
// Polyfill for Promise.all
function myPromiseAll(promises) {
  return new Promise((resolve, reject) => {
    if (!Array.isArray(promises)) {
      return reject(new TypeError('Argument must be an array'));
    }

    const results = [];
    let completed = 0;

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

    promises.forEach((p, index) => {
      Promise.resolve(p)
        .then((val) => {
          results[index] = val;
          completed++;
          if (completed === promises.length) {
            resolve(results);
          }
        })
        .catch(reject);
    });
  });
}

// Test cases
const p1 = Promise.resolve(10);
const p2 = new Promise((res) => setTimeout(() => res('Hello!'), 50));
const p3 = 42;

myPromiseAll([p1, p2, p3])
  .then((data) => console.log('Resolved all:', data))
  .catch((err) => console.error('Rejected:', err));
Console Output
Click "Run Code" above to execute and view output...