| 1 |
// |reftest| skip-if(!xulRuntime.shell) -- needs drainJobQueue |
| 2 |
|
| 3 |
let results = []; |
| 4 |
|
| 5 |
new Promise(res=>res('result')) |
| 6 |
.then(val=>{results.push('then ' + val); return 'first then rval';}) |
| 7 |
.then(val=>results.push('chained then with val: ' + val)); |
| 8 |
|
| 9 |
new Promise((res, rej)=>rej('rejection')) |
| 10 |
.catch(val=>{results.push('catch ' + val); return results.length;}) |
| 11 |
.then(val=>results.push('then after catch with val: ' + val), |
| 12 |
val=>{throw new Error("mustn't be called")}); |
| 13 |
|
| 14 |
new Promise((res, rej)=> {res('result'); rej('rejection'); }) |
| 15 |
.catch(val=>{throw new Error("mustn't be called");}) |
| 16 |
.then(val=>results.push('then after resolve+reject with val: ' + val), |
| 17 |
val=>{throw new Error("mustn't be called")}); |
| 18 |
|
| 19 |
new Promise((res, rej)=> { rej('rejection'); res('result'); }) |
| 20 |
.catch(val=>{results.push('catch after reject+resolve with val: ' + val);}) |
| 21 |
|
| 22 |
|
| 23 |
drainJobQueue(); |
| 24 |
|
| 25 |
assertEq(results.length, 6); |
| 26 |
assertEq(results[0], 'then result'); |
| 27 |
assertEq(results[1], 'catch rejection'); |
| 28 |
assertEq(results[2], 'catch after reject+resolve with val: rejection'); |
| 29 |
assertEq(results[3], 'chained then with val: first then rval'); |
| 30 |
assertEq(results[4], 'then after catch with val: 2'); |
| 31 |
assertEq(results[5], 'then after resolve+reject with val: result'); |
| 32 |
|
| 33 |
results = []; |
| 34 |
|
| 35 |
Promise.resolve('resolution').then(res=>results.push(res), |
| 36 |
rej=>{ throw new Error("mustn't be called"); }); |
| 37 |
|
| 38 |
let thenCalled = false; |
| 39 |
Promise.reject('rejection').then(_=>{thenCalled = true}, |
| 40 |
rej=>results.push(rej)); |
| 41 |
|
| 42 |
drainJobQueue(); |
| 43 |
|
| 44 |
assertEq(thenCalled, false); |
| 45 |
assertEq(results.length, 2); |
| 46 |
assertEq(results[0], 'resolution'); |
| 47 |
assertEq(results[1], 'rejection'); |
| 48 |
|
| 49 |
|
| 50 |
function callback() {} |
| 51 |
|
| 52 |
// Calling the executor function with content functions shouldn't assert: |
| 53 |
Promise.resolve.call(function(exec) { exec(callback, callback); }); |
| 54 |
Promise.reject.call(function(exec) { exec(callback, callback); }); |
| 55 |
Promise.all.call(function(exec) { exec(callback, callback); }); |
| 56 |
Promise.race.call(function(exec) { exec(callback, callback); }); |
| 57 |
|
| 58 |
let resolveResult = undefined; |
| 59 |
function resolveFun() {resolveResult = "resolveCalled";} |
| 60 |
Promise.resolve.call(function(exec) { exec(resolveFun, callback); }); |
| 61 |
assertEq(resolveResult, "resolveCalled"); |
| 62 |
|
| 63 |
let rejectResult = undefined; |
| 64 |
function rejectFun() {rejectResult = "rejectCalled";} |
| 65 |
Promise.reject.call(function(exec) { exec(callback, rejectFun); }); |
| 66 |
assertEq(rejectResult, "rejectCalled"); |
| 67 |
|
| 68 |
// These should throw: |
| 69 |
var wasCalled = false; |
| 70 |
var hasThrown = false; |
| 71 |
try { |
| 72 |
// Calling the executor function twice, providing a resolve callback both times. |
| 73 |
Promise.resolve.call(function(executor) { |
| 74 |
wasCalled = true; |
| 75 |
executor(callback, undefined); |
| 76 |
executor(callback, callback); |
| 77 |
}); |
| 78 |
} catch (e) { |
| 79 |
hasThrown = true; |
| 80 |
} |
| 81 |
assertEq(wasCalled, true); |
| 82 |
assertEq(hasThrown, true); |
| 83 |
|
| 84 |
var hasThrown = false; |
| 85 |
try { |
| 86 |
// Calling the executor function twice, providing a reject callback both times. |
| 87 |
Promise.resolve.call(function(executor) { |
| 88 |
executor(undefined, callback); |
| 89 |
executor(callback, callback); |
| 90 |
}); |
| 91 |
} catch (e) { |
| 92 |
hasThrown = true; |
| 93 |
} |
| 94 |
assertEq(hasThrown, true); |
| 95 |
|
| 96 |
this.reportCompare && reportCompare(0, 0, "ok"); |