forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync-function.js
More file actions
194 lines (162 loc) · 4.55 KB
/
async-function.js
File metadata and controls
194 lines (162 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// RUN: %hermes -Xes6-promise %s | %FileCheck --match-full-lines %s
// RUN: %hermes -O -Xes6-promise %s | %FileCheck --match-full-lines %s
// RUN: %hermes -lazy -Xes6-promise %s | %FileCheck --match-full-lines %s
// Noted that the process of resolving Promise is asynchronous,
// and deeper promise chain will take more microtask ticks to complete.
//
// Hence, the running of tests are ordered from taking less ticks to more ticks
// to make printings conforming with the order of comments in the source code.
print('async function');
// CHECK-LABEL: async function
// CHECK-NEXT: 0
// A pseudo task to simulate an async operation.
function task(v) {
return new Promise((resolve, _) => resolve(v));
}
// --- Fundamentals --- //
async function empty() {};
async function simpleReturn() {
return 1;
}
async function adopted() {
var x = Promise.resolve(1)
return x;
}
async function simpleAwait() {
var x = await 2;
return x;
}
async function sequencing() {
var t1 = await 1;
var t2 = await task(t1 + 1);
var t3 = await task(t2 + 1);
// 3rd tick
print(t3, "sequencing");
return t3;
}
// --- Parameters --- //
async function simpleParam(x) {
var x = await x;
return x;
}
async function nonSimpleDefaultAssign(x=2) {
var x = await x;
return x;
}
async function nonSimpleArrayDestructuring([x]) {
var x = await x;
return x;
}
async function nonSimpleObjDestructuring({x}) {
var x = await x;
return x;
}
async function restParam(...args) {
var x = await args[2];
return x;
}
// --- Error handling --- //
async function simpleThrow() {
throw 1;
}
async function tryCatch() {
try {
throw 1;
} catch (e) {
// same tick
throw e;
}
}
async function tryFinally() {
try {
var t1 = await 1;
var t2 = await task(t1 + 1)
var t3 = await task(t2 + 1)
print(t3, "tryFinally");
} finally {
// 3rd tick
print(3, "tryFinally finally");
}
}
async function tryFinallyReturn() {
try {
var t1 = await 1;
var t2 = await task(t1 + 1)
var t3 = await task(t2 + 1)
// 3rd tick
print(t3, "tryFinallyReturn");
} finally {
// 3rd tick
print(3, "tryFinallyReturn finally");
return t3;
}
}
async function tryCatchFinally() {
try {
var t1 = await 1;
var t2 = await task(t1 + 1)
var t3 = await task(t2 + 1)
// 3rd tick
print(t3, "tryCatchFinally");
throw t3;
} catch (e) {
// 3rd tick
print(t3, "tryCatchFinally catch");
throw e;
} finally {
// 3rd tick
print(3, "tryCatchFinally finally");
}
}
// --- One ticks --- //
empty().then(v => print(v, "empty"));
// CHECK-NEXT: undefined empty
simpleReturn().then(v => print(v, "simpleReturn"));
// CHECK-NEXT: 1 simpleReturn
adopted().then(v => print(v, "adopted"));
// CHECK-NEXT: 1 adopted
tryCatch().catch(e => print(e, "tryCatch"));
// CHECK-NEXT: 1 tryCatch
simpleThrow().catch(e => print(e, "simpleThrow"));
// CHECK-NEXT: 1 simpleThrow
// --- Two ticks --- //
simpleAwait().then(v => print(v, "simpleAwait"));
// CHECK-NEXT: 2 simpleAwait
simpleParam(2).then(v => print(v, "simpleParam"));
// CHECK-NEXT: 2 simpleParam
nonSimpleDefaultAssign(2).then(v => print(v, "nonSimpleDefaultAssign"));
// CHECK-NEXT: 2 nonSimpleDefaultAssign
nonSimpleArrayDestructuring([2]).then(v => print(v, "nonSimpleArrayDestructuring"));
// CHECK-NEXT: 2 nonSimpleArrayDestructuring
nonSimpleObjDestructuring({x:2}).then(v => print(v, "nonSimpleObjDestructuring"));
// CHECK-NEXT: 2 nonSimpleObjDestructuring
restParam(0, 1, 2).then(v => print(v, "restParam"));
// CHECK-NEXT: 2 restParam
// --- Three ticks --- //
// print inside async function body goes into thc 3rd tick,
// print inside `then`/`catch` callback goes into the 4th tick.
sequencing().then(v => print(v+1, "sequencing cb"));
// CHECK-NEXT: 3 sequencing
tryFinally().then(v => print(v, "tryFinally cb"))
// CHECK-NEXT: 3 tryFinally
// CHECK-NEXT: 3 tryFinally finally
tryFinallyReturn().then(v => print(v + 1, "tryFinallyReturn cb"));
// CHECK-NEXT: 3 tryFinallyReturn
// CHECK-NEXT: 3 tryFinallyReturn finally
tryCatchFinally().catch(v => print(v + 1, "tryCatchFinally cb"));
// CHECK-NEXT: 3 tryCatchFinally
// CHECK-NEXT: 3 tryCatchFinally catch
// CHECK-NEXT: 3 tryCatchFinally finally
// --- Four ticks --- //
// CHECK-NEXT: 4 sequencing cb
// CHECK-NEXT: undefined tryFinally cb
// CHECK-NEXT: 4 tryFinallyReturn cb
// CHECK-NEXT: 4 tryCatchFinally cb
// --- Zero ticks --- //
print("0")