1
1
// Boilerplate currently handwritten
2
- external async0 : Js .Fn .arity0 <'r > => Js .Fn .arity0 <Js .Promise .t <'r >> = "?async"
3
- external async1 : Js .Fn .arity1 <'a1 => 'r > => Js .Fn .arity1 <'a1 => Js .Promise .t <'r >> = "?async"
4
- external async2 : Js .Fn .arity2 <('a1 , 'a2 ) => 'r > => Js .Fn .arity2 <('a1 , 'a2 ) => Js .Promise .t <'r >> =
5
- "?async"
6
-
7
2
external await : Js .Promise .t <'a > => 'a = "?await"
8
3
9
4
type testable = (. unit ) => Js .Promise .t <unit >
@@ -17,20 +12,24 @@ let addTest1 = (t, x) => tests->Js.Array2.push((. ()) => t(. x))->ignore
17
12
//
18
13
// Basic tests
19
14
20
- let foo = async2 (( . x , y ) => x + y )
15
+ let foo = @ async ( . x , y ) => x + y
21
16
22
- let bar = async1 ((. ff ) => {
23
- let a = await (ff (. 3 , 4 ))
24
- let b = await (foo (. 5 , 6 ))
25
- a + b
26
- })
17
+ let bar =
18
+ @async
19
+ (. ff ) => {
20
+ let a = await (ff (. 3 , 4 ))
21
+ let b = await (foo (. 5 , 6 ))
22
+ a + b
23
+ }
27
24
28
- let baz = async0 (( . ()) => await (bar (. foo ) ))
25
+ let baz = @ async ( . ()) => await (bar (. foo ))
29
26
30
- let testBaz : testable = async0 ((. ()) => {
31
- let n = await (baz (.))
32
- Js .log2 ("baz returned" , n )
33
- })
27
+ let testBaz : testable =
28
+ @async
29
+ (. ()) => {
30
+ let n = await (baz (.))
31
+ Js .log2 ("baz returned" , n )
32
+ }
34
33
35
34
testBaz -> addTest
36
35
@@ -40,18 +39,19 @@ testBaz->addTest
40
39
41
40
exception E (int )
42
41
43
- let e1 : testable = async0 (( . ()) => raise (E (1000 ) ))
44
- let e2 : testable = async0 (( . ()) => Js .Exn .raiseError ("Some JS error" ) )
45
- let e3 : testable = async0 (( . ()) => await (e1 (.) ))
46
- let e4 : testable = async0 (( . ()) => await (e2 (.) ))
42
+ let e1 : testable = @ async ( . ()) => raise (E (1000 ))
43
+ let e2 : testable = @ async ( . ()) => Js .Exn .raiseError ("Some JS error" )
44
+ let e3 : testable = @ async ( . ()) => await (e1 (.))
45
+ let e4 : testable = @ async ( . ()) => await (e2 (.))
47
46
let e5 : testable = %raw (` function () { return Promise .reject (new Error (' fail' )) }` )
48
47
49
- let testTryCatch = async1 ((. fn ) =>
50
- try await (fn (.)) catch {
51
- | E (n ) => Js .log2 ("testTryCatch: E" , n )
52
- | JsError (_ ) => Js .log ("testTryCatch: JsError" )
53
- }
54
- )
48
+ let testTryCatch =
49
+ @async
50
+ (. fn ) =>
51
+ try await (fn (.)) catch {
52
+ | E (n ) => Js .log2 ("testTryCatch: E" , n )
53
+ | JsError (_ ) => Js .log ("testTryCatch: JsError" )
54
+ }
55
55
56
56
testTryCatch -> addTest1 (e1 )
57
57
testTryCatch -> addTest1 (e2 )
@@ -63,28 +63,32 @@ testTryCatch->addTest1(e5)
63
63
//
64
64
// Check for nested promise
65
65
66
- let singlePromise = async1 (( . x ) => x + 1 )
66
+ let singlePromise = @ async ( . x ) => x + 1
67
67
68
- let nestedPromise = async1 ((. x ) => {
69
- let resolve = x => [Js .Promise .resolve (x )]
70
- let _result = singlePromise (. x + 1 )-> resolve
71
- 32
72
- })
68
+ let nestedPromise =
69
+ @async
70
+ (. x ) => {
71
+ let resolve = x => [Js .Promise .resolve (x )]
72
+ let _result = singlePromise (. x + 1 )-> resolve
73
+ 32
74
+ }
73
75
74
76
//
75
77
//
76
78
// Test error handling in fetch
77
79
78
80
let explainError : unknown => string = %raw (` (e )=> e .toString ()` )
79
81
80
- let testFetch = async1 ((. url ) => {
81
- switch await (Fetch .fetch (url )) {
82
- | response =>
83
- let status = response -> Fetch .Response .status
84
- Js .log2 ("Fetch returned status:" , status )
85
- | exception JsError (e ) => Js .log2 ("Fetch returned an error:" , e -> explainError )
82
+ let testFetch =
83
+ @async
84
+ (. url ) => {
85
+ switch await (Fetch .fetch (url )) {
86
+ | response =>
87
+ let status = response -> Fetch .Response .status
88
+ Js .log2 ("Fetch returned status:" , status )
89
+ | exception JsError (e ) => Js .log2 ("Fetch returned an error:" , e -> explainError )
90
+ }
86
91
}
87
- })
88
92
89
93
testFetch -> addTest1 ("https://www.google.com/sdkjdkghdsg" )
90
94
testFetch -> addTest1 ("https://www.google.comsdkjdkghdsg" )
@@ -93,12 +97,14 @@ testFetch->addTest1("https://www.google.comsdkjdkghdsg")
93
97
//
94
98
// Run tests
95
99
96
- let runAllTests = async0 ((. ()) => {
97
- for i in 0 to Array .length (tests ) - 1 {
98
- await (tests [i ](.))
100
+ let runAllTests =
101
+ @async
102
+ (. ()) => {
103
+ for i in 0 to Array .length (tests ) - 1 {
104
+ await (tests [i ](.))
105
+ }
106
+ // Note: this is no good, as await is inside a closure
107
+ // tests->Js.Array2.forEach(test => await(test(.)))
99
108
}
100
- // Note: this is no good, as await is inside a closure
101
- // tests->Js.Array2.forEach(test => await(test(.)))
102
- })
103
109
104
110
runAllTests (.)-> ignore
0 commit comments