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