MCPcopy Create free account
hub / github.com/Faheem-Khan97/javascript-coding-problems-with-theory / myPromise

Class myPromise

polyfills/promise-methods.js:17–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15};
16
17class myPromise {
18 static all(thenableList) {
19 return new Promise((resolve, reject) => {
20 const res = new Array(thenableList.length);
21 let pending = res.length;
22 thenableList.forEach((promise, index) => {
23 promise.then(
24 (value) => {
25 res[index] = value;
26 pending--;
27 if (pending == 0) {
28 resolve(res);
29 }
30 },
31 (err) => {
32 reject(err);
33 }
34 );
35 });
36 });
37 }
38
39 static allSettled(thenableList) {
40 return new Promise((resolve, reject) => {
41 const res = new Array(thenableList.length);
42 let pending = res.length;
43 thenableList.forEach((promise, index) => {
44 promise
45 .then((value) => {
46 res[index] = {
47 status: "fulfilled",
48 value: value,
49 };
50 })
51 .catch((err) => {
52 res[index] = {
53 status: "rejected",
54 reason: err,
55 };
56 })
57 .finally(() => {
58 pending--;
59 if (pending == 0) {
60 resolve(res);
61 }
62 });
63 });
64 });
65 }
66
67 static any(thenableList) {
68 return new Promise((resolve, reject) => {
69 if (thenableList.length == 0) {
70 reject({ status: "rejected", reason: "empty list" });
71 } else {
72 const aggregate = {
73 status: "rejected",
74 reason: [],

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected