forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync-function-expr.js
More file actions
123 lines (98 loc) · 2.59 KB
/
async-function-expr.js
File metadata and controls
123 lines (98 loc) · 2.59 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
/**
* 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 expression');
// CHECK-LABEL: async function expression
// CHECK-NEXT: 0
// A pseudo task to simulate an async operation.
function task(v) {
return new Promise((resolve, _) => resolve(v));
}
// --- One ticks --- //
(async function () {})().then(v => print(v));
// CHECK-NEXT: undefined
(async function simpleReturn() {
return 1;
})().then(v => print(v));
// CHECK-NEXT: 1
(async function adopted() {
var x = Promise.resolve(1)
return x;
})().then(v => print(v));
// CHECK-NEXT: 1
(async function tryCatch() {
try {
throw 0;
} catch (e) {
throw e+1;
}
})().catch(v => print(v));
// CHECK-NEXT: 1
(async function simpleThrow() {
throw 1;
})().catch(v => print(v));
// CHECK-NEXT: 1
// --- Two ticks --- //
(async function simpleParam(x) {
var x = await x;
return x;
})(2).then(v => print(v));
// CHECK-NEXT: 2
(async function simpleAwait() {
var x = await 2;
return x;
})().then(v => print(v));
// CHECK-NEXT: 2
(async function nonSimpleArrayDestructuring([x]) {
var x = await x;
return x;
})([2]).then(v => print(v));
// CHECK-NEXT: 2
// --- Three ticks --- //
// print inside async function body goes into thc 3rd tick,
// print inside `then`/`catch` callback goes into the 4th tick.
(async function sequencing() {
var t1 = await 1;
var t2 = await task(t1 + 1);
var t3 = await task(t2 + 1);
// 3rd tick
print(t3);
return t3;
})().then(v => print(v+1));
// CHECK-NEXT: 3
(async function tryCatchFinally() {
try {
var t1 = await 1;
var t2 = await task(t1 + 1)
var t3 = await task(t2 + 1)
// 3rd tick
print(t3);
throw t3;
} catch (e) {
// 3rd tick
print(t3);
throw e;
} finally {
// 3rd tick
print("tryCatchFinally");
}
})().catch(v => print(v+1));
// CHECK-NEXT: 3
// CHECK-NEXT: 3
// CHECK-NEXT: tryCatchFinally
// --- Four ticks --- //
// CHECK-NEXT: 4
// CHECK-NEXT: 4
// --- Zero ticks --- //
print("0")