马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const t = require('tap')
 
const main = () => {
  if (process.argv[2] === 'polyfill-all-settled') {
    Promise.allSettled = null
    runTests()
  } else if (process.argv[2] === 'native-all-settled') {
    Promise.allSettled = Promise.allSettled || (
      promises => {
        const reflections = []
        for (let i = 0; i < promises.length; i++) {
          reflections[i] = Promise.resolve(promises[i]).then(value => ({
            status: 'fulfilled',
            value,
          }), reason => ({
            status: 'rejected',
            reason,
          }))
        }
        return Promise.all(reflections)
      }
    )
    runTests()
  } else {
    t.spawn(process.execPath, [__filename, 'polyfill-all-settled'])
    t.spawn(process.execPath, [__filename, 'native-all-settled'])
  }
}
 
const runTests = () => {
  const lateFail = require('../')
 
  t.test('fail only after all promises resolve', t => {
    let resolvedSlow = false
    const fast = () => Promise.reject('nope')
    const slow = () => new Promise(res => setTimeout(res, 100))
      .then(() => resolvedSlow = true)
 
    // throw some holes and junk in the array to verify that we handle it
    return t.rejects(lateFail([fast(),,,,slow(), null, {not: 'a promise'},,,]))
      .then(() => t.equal(resolvedSlow, true, 'resolved slow before failure'))
  })
 
  t.test('works just like Promise.all() otherwise', t => {
    const one = () => Promise.resolve(1)
    const two = () => Promise.resolve(2)
    const tre = () => Promise.resolve(3)
    const fur = () => Promise.resolve(4)
    const fiv = () => Promise.resolve(5)
    const six = () => Promise.resolve(6)
    const svn = () => Promise.resolve(7)
    const eit = () => Promise.resolve(8)
    const nin = () => Promise.resolve(9)
    const ten = () => Promise.resolve(10)
    const expect = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    const all = Promise.all([
      one(),
      two(),
      tre(),
      fur(),
      fiv(),
      six(),
      svn(),
      eit(),
      nin(),
      ten(),
    ])
    const late = lateFail([
      one(),
      two(),
      tre(),
      fur(),
      fiv(),
      six(),
      svn(),
      eit(),
      nin(),
      ten(),
    ])
 
    return Promise.all([all, late]).then(([all, late]) => {
      t.strictSame(all, expect)
      t.strictSame(late, expect)
    })
  })
}
 
main()