polyfill.ts raw
1 if (!Array.prototype.findLast) {
2 Array.prototype.findLast = function <T>(
3 predicate: (value: T, index: number, obj: T[]) => boolean,
4 thisArg?: any
5 ): T | undefined {
6 if (this == null) {
7 throw new TypeError('Array.prototype.findLast called on null or undefined')
8 }
9 if (typeof predicate !== 'function') {
10 throw new TypeError('predicate must be a function')
11 }
12 const list = Object(this)
13 const length = list.length >>> 0
14 let value: T
15
16 for (let i = length - 1; i >= 0; i--) {
17 value = list[i]
18 if (predicate.call(thisArg, value, i, list)) {
19 return value
20 }
21 }
22 return undefined
23 }
24 }
25
26 if (typeof AggregateError === 'undefined') {
27 class AggregateError extends Error {
28 errors: any[]
29
30 constructor(errors: any[], message?: string) {
31 super(message)
32 this.errors = errors
33 this.name = 'AggregateError'
34 }
35 }
36
37 ;(globalThis as any).AggregateError = AggregateError
38 }
39
40 if (!Promise.any) {
41 Promise.any = function (promises) {
42 return new Promise((resolve, reject) => {
43 const errors: any[] = []
44 let pending = promises.length
45
46 if (pending === 0) {
47 return reject(new AggregateError([], 'All promises were rejected'))
48 }
49
50 promises.forEach((promise, index) => {
51 Promise.resolve(promise)
52 .then(resolve)
53 .catch((error) => {
54 errors[index] = error
55 pending -= 1
56 if (pending === 0) {
57 reject(new AggregateError(errors, 'All promises were rejected'))
58 }
59 })
60 })
61 })
62 }
63 }
64