11cb0ef41Sopenharmony_ciconst t = require('tap') 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst main = () => { 41cb0ef41Sopenharmony_ci if (process.argv[2] === 'polyfill-all-settled') { 51cb0ef41Sopenharmony_ci Promise.allSettled = null 61cb0ef41Sopenharmony_ci runTests() 71cb0ef41Sopenharmony_ci } else if (process.argv[2] === 'native-all-settled') { 81cb0ef41Sopenharmony_ci Promise.allSettled = Promise.allSettled || ( 91cb0ef41Sopenharmony_ci promises => { 101cb0ef41Sopenharmony_ci const reflections = [] 111cb0ef41Sopenharmony_ci for (let i = 0; i < promises.length; i++) { 121cb0ef41Sopenharmony_ci reflections[i] = Promise.resolve(promises[i]).then(value => ({ 131cb0ef41Sopenharmony_ci status: 'fulfilled', 141cb0ef41Sopenharmony_ci value, 151cb0ef41Sopenharmony_ci }), reason => ({ 161cb0ef41Sopenharmony_ci status: 'rejected', 171cb0ef41Sopenharmony_ci reason, 181cb0ef41Sopenharmony_ci })) 191cb0ef41Sopenharmony_ci } 201cb0ef41Sopenharmony_ci return Promise.all(reflections) 211cb0ef41Sopenharmony_ci } 221cb0ef41Sopenharmony_ci ) 231cb0ef41Sopenharmony_ci runTests() 241cb0ef41Sopenharmony_ci } else { 251cb0ef41Sopenharmony_ci t.spawn(process.execPath, [__filename, 'polyfill-all-settled']) 261cb0ef41Sopenharmony_ci t.spawn(process.execPath, [__filename, 'native-all-settled']) 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciconst runTests = () => { 311cb0ef41Sopenharmony_ci const lateFail = require('../') 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci t.test('fail only after all promises resolve', t => { 341cb0ef41Sopenharmony_ci let resolvedSlow = false 351cb0ef41Sopenharmony_ci const fast = () => Promise.reject('nope') 361cb0ef41Sopenharmony_ci const slow = () => new Promise(res => setTimeout(res, 100)) 371cb0ef41Sopenharmony_ci .then(() => resolvedSlow = true) 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci // throw some holes and junk in the array to verify that we handle it 401cb0ef41Sopenharmony_ci return t.rejects(lateFail([fast(),,,,slow(), null, {not: 'a promise'},,,])) 411cb0ef41Sopenharmony_ci .then(() => t.equal(resolvedSlow, true, 'resolved slow before failure')) 421cb0ef41Sopenharmony_ci }) 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci t.test('works just like Promise.all() otherwise', t => { 451cb0ef41Sopenharmony_ci const one = () => Promise.resolve(1) 461cb0ef41Sopenharmony_ci const two = () => Promise.resolve(2) 471cb0ef41Sopenharmony_ci const tre = () => Promise.resolve(3) 481cb0ef41Sopenharmony_ci const fur = () => Promise.resolve(4) 491cb0ef41Sopenharmony_ci const fiv = () => Promise.resolve(5) 501cb0ef41Sopenharmony_ci const six = () => Promise.resolve(6) 511cb0ef41Sopenharmony_ci const svn = () => Promise.resolve(7) 521cb0ef41Sopenharmony_ci const eit = () => Promise.resolve(8) 531cb0ef41Sopenharmony_ci const nin = () => Promise.resolve(9) 541cb0ef41Sopenharmony_ci const ten = () => Promise.resolve(10) 551cb0ef41Sopenharmony_ci const expect = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 561cb0ef41Sopenharmony_ci const all = Promise.all([ 571cb0ef41Sopenharmony_ci one(), 581cb0ef41Sopenharmony_ci two(), 591cb0ef41Sopenharmony_ci tre(), 601cb0ef41Sopenharmony_ci fur(), 611cb0ef41Sopenharmony_ci fiv(), 621cb0ef41Sopenharmony_ci six(), 631cb0ef41Sopenharmony_ci svn(), 641cb0ef41Sopenharmony_ci eit(), 651cb0ef41Sopenharmony_ci nin(), 661cb0ef41Sopenharmony_ci ten(), 671cb0ef41Sopenharmony_ci ]) 681cb0ef41Sopenharmony_ci const late = lateFail([ 691cb0ef41Sopenharmony_ci one(), 701cb0ef41Sopenharmony_ci two(), 711cb0ef41Sopenharmony_ci tre(), 721cb0ef41Sopenharmony_ci fur(), 731cb0ef41Sopenharmony_ci fiv(), 741cb0ef41Sopenharmony_ci six(), 751cb0ef41Sopenharmony_ci svn(), 761cb0ef41Sopenharmony_ci eit(), 771cb0ef41Sopenharmony_ci nin(), 781cb0ef41Sopenharmony_ci ten(), 791cb0ef41Sopenharmony_ci ]) 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci return Promise.all([all, late]).then(([all, late]) => { 821cb0ef41Sopenharmony_ci t.strictSame(all, expect) 831cb0ef41Sopenharmony_ci t.strictSame(late, expect) 841cb0ef41Sopenharmony_ci }) 851cb0ef41Sopenharmony_ci }) 861cb0ef41Sopenharmony_ci} 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_cimain() 89