1
\$\begingroup\$

I took a stab at implementing Promise.race():

"use strict";

function race (...promises) {
   return new Promise((resolve, reject) => {
      let [resolved, rejected] = [false, false];
      promises.forEach(promise => {
         promise.then(value => {
            if (!resolved) {
               resolve(value);
               resolved = true;
            }
         }, 
         reason => {
            if (!rejected) {
               reject(reason);
               rejected = true;
            }
         })
      });
   });
}
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Given the issue I described when I tried to run your code for Promise.all() in my answer for that post, please edit your post to include sample inputs and expected/actual output- perhaps with a runnable snippet... \$\endgroup\$ Commented Feb 12, 2020 at 14:39
  • \$\begingroup\$ I’m voting to close this as not working because in this sample bin that uses the code above there is an error in the console TypeError: promise.then is not a function \$\endgroup\$ Commented Feb 27, 2020 at 6:52

0