11'use strict' ;
22const test_promise = loadAddon ( 'test_promise' ) ;
33
4- const tick = ( ) => new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
5-
64// A resolution
75{
86 const expected_result = 42 ;
97 const promise = test_promise . createPromise ( ) ;
10- let resolved = false ;
11- promise . then ( ( result ) => {
8+ const [ onResolve , resolved ] = mustCall ( ( result ) => {
129 assert . strictEqual ( result , expected_result ) ;
13- resolved = true ;
1410 } ) ;
11+ promise . then ( onResolve ) ;
1512 test_promise . concludeCurrentPromise ( expected_result , true ) ;
16- await tick ( ) ;
17- assert ( resolved , 'resolve callback was not called' ) ;
13+ await resolved ;
1814}
1915
2016// A rejection
2117{
2218 const expected_result = 'It\'s not you, it\'s me.' ;
2319 const promise = test_promise . createPromise ( ) ;
24- let rejected = false ;
25- let thenCalled = false ;
26- promise . then (
27- ( ) => { throw new Error ( 'unexpected resolve' ) ; } ,
28- ( result ) => {
29- assert . strictEqual ( result , expected_result ) ;
30- rejected = true ;
31- } ,
32- ) . then ( ( ) => { thenCalled = true ; } ) ;
20+ const [ onReject , rejected ] = mustCall ( ( result ) => {
21+ assert . strictEqual ( result , expected_result ) ;
22+ } ) ;
23+ const [ onThen , thenCalled ] = mustCall ( ) ;
24+ promise . then ( mustNotCall ( ) , onReject ) . then ( onThen ) ;
3325 test_promise . concludeCurrentPromise ( expected_result , false ) ;
34- await tick ( ) ;
35- assert ( rejected , 'reject callback was not called' ) ;
36- assert ( thenCalled , 'then after catch was not called' ) ;
26+ await thenCalled ;
3727}
3828
3929// Chaining
4030{
4131 const expected_result = 'chained answer' ;
4232 const promise = test_promise . createPromise ( ) ;
43- let resolved = false ;
44- promise . then ( ( result ) => {
33+ const [ onResolve , resolved ] = mustCall ( ( result ) => {
4534 assert . strictEqual ( result , expected_result ) ;
46- resolved = true ;
4735 } ) ;
36+ promise . then ( onResolve ) ;
4837 test_promise . concludeCurrentPromise ( Promise . resolve ( 'chained answer' ) , true ) ;
49- await tick ( ) ;
50- assert ( resolved , 'chaining resolve callback was not called' ) ;
38+ await resolved ;
5139}
5240
5341const promiseTypeTestPromise = test_promise . createPromise ( ) ;
@@ -57,13 +45,11 @@ test_promise.concludeCurrentPromise(undefined, true);
5745const rejectPromise = Promise . reject ( - 1 ) ;
5846const expected_reason = - 1 ;
5947assert . strictEqual ( test_promise . isPromise ( rejectPromise ) , true ) ;
60- let caught = false ;
61- rejectPromise . catch ( ( reason ) => {
48+ const [ onCatch , caught ] = mustCall ( ( reason ) => {
6249 assert . strictEqual ( reason , expected_reason ) ;
63- caught = true ;
6450} ) ;
65- await tick ( ) ;
66- assert ( caught , 'catch was not called' ) ;
51+ rejectPromise . catch ( onCatch ) ;
52+ await caught ;
6753
6854assert . strictEqual ( test_promise . isPromise ( 2.4 ) , false ) ;
6955assert . strictEqual ( test_promise . isPromise ( 'I promise!' ) , false ) ;
0 commit comments