Skip to content

Commit f17bf54

Browse files
authored
Merge pull request microsoft#23887 from agentcooper/async-generator-resolve-promise
Resolve promise inside async generator
2 parents 030b8fc + d88210b commit f17bf54

14 files changed

+150
-115
lines changed

src/compiler/transformers/esnext.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,28 +118,46 @@ namespace ts {
118118
}
119119

120120
function visitYieldExpression(node: YieldExpression) {
121-
if (enclosingFunctionFlags & FunctionFlags.Async && enclosingFunctionFlags & FunctionFlags.Generator && node.asteriskToken) {
122-
const expression = visitNode(node.expression, visitor, isExpression);
121+
if (enclosingFunctionFlags & FunctionFlags.Async && enclosingFunctionFlags & FunctionFlags.Generator) {
122+
if (node.asteriskToken) {
123+
const expression = visitNode(node.expression, visitor, isExpression);
124+
125+
return setOriginalNode(
126+
setTextRange(
127+
createYield(
128+
createAwaitHelper(context,
129+
updateYield(
130+
node,
131+
node.asteriskToken,
132+
createAsyncDelegatorHelper(
133+
context,
134+
createAsyncValuesHelper(context, expression, expression),
135+
expression
136+
)
137+
)
138+
)
139+
),
140+
node
141+
),
142+
node
143+
);
144+
}
145+
123146
return setOriginalNode(
124147
setTextRange(
125148
createYield(
126-
createAwaitHelper(context,
127-
updateYield(
128-
node,
129-
node.asteriskToken,
130-
createAsyncDelegatorHelper(
131-
context,
132-
createAsyncValuesHelper(context, expression, expression),
133-
expression
134-
)
135-
)
149+
createDownlevelAwait(
150+
node.expression
151+
? visitNode(node.expression, visitor, isExpression)
152+
: createVoidZero()
136153
)
137154
),
138155
node
139156
),
140157
node
141158
);
142159
}
160+
143161
return visitEachChild(node, visitor, context);
144162
}
145163

tests/baselines/reference/asyncImportNestedYield.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ function foo() {
4747
return __asyncGenerator(this, arguments, function foo_1() {
4848
return __generator(this, function (_a) {
4949
switch (_a.label) {
50-
case 0: return [4 /*yield*/, "foo"];
51-
case 1: return [4 /*yield*/, __await.apply(void 0, [Promise.resolve().then(function () { return require(_a.sent()); })])];
52-
case 2:
50+
case 0: return [4 /*yield*/, __await("foo")];
51+
case 1: return [4 /*yield*/, _a.sent()];
52+
case 2: return [4 /*yield*/, __await.apply(void 0, [Promise.resolve().then(function () { return require(_a.sent()); })])];
53+
case 3:
5354
Promise.resolve().then(function () { return require((_a.sent())["default"]); });
5455
return [2 /*return*/];
5556
}

tests/baselines/reference/emitter.asyncGenerators.classMethods.es2015.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
9595
class C2 {
9696
f() {
9797
return __asyncGenerator(this, arguments, function* f_1() {
98-
const x = yield;
98+
const x = yield yield __await(void 0);
9999
});
100100
}
101101
}
@@ -115,7 +115,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
115115
class C3 {
116116
f() {
117117
return __asyncGenerator(this, arguments, function* f_1() {
118-
const x = yield 1;
118+
const x = yield yield __await(1);
119119
});
120120
}
121121
}
@@ -175,7 +175,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
175175
class C5 {
176176
f() {
177177
return __asyncGenerator(this, arguments, function* f_1() {
178-
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
178+
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
179179
});
180180
}
181181
}

tests/baselines/reference/emitter.asyncGenerators.classMethods.es5.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ var C2 = /** @class */ (function () {
160160
var x;
161161
return __generator(this, function (_a) {
162162
switch (_a.label) {
163-
case 0: return [4 /*yield*/];
164-
case 1:
163+
case 0: return [4 /*yield*/, __await(void 0)];
164+
case 1: return [4 /*yield*/, _a.sent()];
165+
case 2:
165166
x = _a.sent();
166167
return [2 /*return*/];
167168
}
@@ -218,8 +219,9 @@ var C3 = /** @class */ (function () {
218219
var x;
219220
return __generator(this, function (_a) {
220221
switch (_a.label) {
221-
case 0: return [4 /*yield*/, 1];
222-
case 1:
222+
case 0: return [4 /*yield*/, __await(1)];
223+
case 1: return [4 /*yield*/, _a.sent()];
224+
case 2:
223225
x = _a.sent();
224226
return [2 /*return*/];
225227
}
@@ -377,8 +379,9 @@ var C5 = /** @class */ (function () {
377379
switch (_a.label) {
378380
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
379381
switch (_a.label) {
380-
case 0: return [4 /*yield*/, 1];
381-
case 1:
382+
case 0: return [4 /*yield*/, __await(1)];
383+
case 1: return [4 /*yield*/, _a.sent()];
384+
case 2:
382385
_a.sent();
383386
return [2 /*return*/];
384387
}

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.es2015.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
6161
};
6262
function f2() {
6363
return __asyncGenerator(this, arguments, function* f2_1() {
64-
const x = yield;
64+
const x = yield yield __await(void 0);
6565
});
6666
}
6767
//// [F3.js]
@@ -79,7 +79,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
7979
};
8080
function f3() {
8181
return __asyncGenerator(this, arguments, function* f3_1() {
82-
const x = yield 1;
82+
const x = yield yield __await(1);
8383
});
8484
}
8585
//// [F4.js]
@@ -135,7 +135,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
135135
};
136136
function f5() {
137137
return __asyncGenerator(this, arguments, function* f5_1() {
138-
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
138+
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
139139
});
140140
}
141141
//// [F6.js]

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.es5.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ function f2() {
121121
var x;
122122
return __generator(this, function (_a) {
123123
switch (_a.label) {
124-
case 0: return [4 /*yield*/];
125-
case 1:
124+
case 0: return [4 /*yield*/, __await(void 0)];
125+
case 1: return [4 /*yield*/, _a.sent()];
126+
case 2:
126127
x = _a.sent();
127128
return [2 /*return*/];
128129
}
@@ -174,8 +175,9 @@ function f3() {
174175
var x;
175176
return __generator(this, function (_a) {
176177
switch (_a.label) {
177-
case 0: return [4 /*yield*/, 1];
178-
case 1:
178+
case 0: return [4 /*yield*/, __await(1)];
179+
case 1: return [4 /*yield*/, _a.sent()];
180+
case 2:
179181
x = _a.sent();
180182
return [2 /*return*/];
181183
}
@@ -323,8 +325,9 @@ function f5() {
323325
switch (_a.label) {
324326
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
325327
switch (_a.label) {
326-
case 0: return [4 /*yield*/, 1];
327-
case 1:
328+
case 0: return [4 /*yield*/, __await(1)];
329+
case 1: return [4 /*yield*/, _a.sent()];
330+
case 2:
328331
_a.sent();
329332
return [2 /*return*/];
330333
}

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.es2015.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
6161
};
6262
const f2 = function () {
6363
return __asyncGenerator(this, arguments, function* () {
64-
const x = yield;
64+
const x = yield yield __await(void 0);
6565
});
6666
};
6767
//// [F3.js]
@@ -79,7 +79,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
7979
};
8080
const f3 = function () {
8181
return __asyncGenerator(this, arguments, function* () {
82-
const x = yield 1;
82+
const x = yield yield __await(1);
8383
});
8484
};
8585
//// [F4.js]
@@ -135,7 +135,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
135135
};
136136
const f5 = function () {
137137
return __asyncGenerator(this, arguments, function* () {
138-
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
138+
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
139139
});
140140
};
141141
//// [F6.js]

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.es5.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ var f2 = function () {
121121
var x;
122122
return __generator(this, function (_a) {
123123
switch (_a.label) {
124-
case 0: return [4 /*yield*/];
125-
case 1:
124+
case 0: return [4 /*yield*/, __await(void 0)];
125+
case 1: return [4 /*yield*/, _a.sent()];
126+
case 2:
126127
x = _a.sent();
127128
return [2 /*return*/];
128129
}
@@ -174,8 +175,9 @@ var f3 = function () {
174175
var x;
175176
return __generator(this, function (_a) {
176177
switch (_a.label) {
177-
case 0: return [4 /*yield*/, 1];
178-
case 1:
178+
case 0: return [4 /*yield*/, __await(1)];
179+
case 1: return [4 /*yield*/, _a.sent()];
180+
case 2:
179181
x = _a.sent();
180182
return [2 /*return*/];
181183
}
@@ -323,8 +325,9 @@ var f5 = function () {
323325
switch (_a.label) {
324326
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
325327
switch (_a.label) {
326-
case 0: return [4 /*yield*/, 1];
327-
case 1:
328+
case 0: return [4 /*yield*/, __await(1)];
329+
case 1: return [4 /*yield*/, _a.sent()];
330+
case 2:
328331
_a.sent();
329332
return [2 /*return*/];
330333
}

tests/baselines/reference/emitter.asyncGenerators.objectLiteralMethods.es2015.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
7878
const o2 = {
7979
f() {
8080
return __asyncGenerator(this, arguments, function* f_1() {
81-
const x = yield;
81+
const x = yield yield __await(void 0);
8282
});
8383
}
8484
};
@@ -98,7 +98,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
9898
const o3 = {
9999
f() {
100100
return __asyncGenerator(this, arguments, function* f_1() {
101-
const x = yield 1;
101+
const x = yield yield __await(1);
102102
});
103103
}
104104
};
@@ -158,7 +158,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
158158
const o5 = {
159159
f() {
160160
return __asyncGenerator(this, arguments, function* f_1() {
161-
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
161+
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
162162
});
163163
}
164164
};

tests/baselines/reference/emitter.asyncGenerators.objectLiteralMethods.es5.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ var o2 = {
138138
var x;
139139
return __generator(this, function (_a) {
140140
switch (_a.label) {
141-
case 0: return [4 /*yield*/];
142-
case 1:
141+
case 0: return [4 /*yield*/, __await(void 0)];
142+
case 1: return [4 /*yield*/, _a.sent()];
143+
case 2:
143144
x = _a.sent();
144145
return [2 /*return*/];
145146
}
@@ -193,8 +194,9 @@ var o3 = {
193194
var x;
194195
return __generator(this, function (_a) {
195196
switch (_a.label) {
196-
case 0: return [4 /*yield*/, 1];
197-
case 1:
197+
case 0: return [4 /*yield*/, __await(1)];
198+
case 1: return [4 /*yield*/, _a.sent()];
199+
case 2:
198200
x = _a.sent();
199201
return [2 /*return*/];
200202
}
@@ -346,8 +348,9 @@ var o5 = {
346348
switch (_a.label) {
347349
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
348350
switch (_a.label) {
349-
case 0: return [4 /*yield*/, 1];
350-
case 1:
351+
case 0: return [4 /*yield*/, __await(1)];
352+
case 1: return [4 /*yield*/, _a.sent()];
353+
case 2:
351354
_a.sent();
352355
return [2 /*return*/];
353356
}

tests/baselines/reference/emitter.forAwait.es2015.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function f3() {
128128
let y;
129129
try {
130130
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
131-
const x = yield __await(y_1_1.value);
131+
const x = yield yield __await(__await(y_1_1.value));
132132
}
133133
}
134134
catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -164,7 +164,7 @@ function f4() {
164164
let x, y;
165165
try {
166166
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
167-
x = yield __await(y_1_1.value);
167+
x = yield yield __await(__await(y_1_1.value));
168168
}
169169
}
170170
catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -235,7 +235,7 @@ function f6() {
235235
let y;
236236
try {
237237
outer: for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
238-
const x = yield __await(y_1_1.value);
238+
const x = yield yield __await(__await(y_1_1.value));
239239
continue outer;
240240
}
241241
}

tests/baselines/reference/emitter.forAwait.es2017.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function f3() {
108108
let y;
109109
try {
110110
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
111-
const x = yield __await(y_1_1.value);
111+
const x = yield yield __await(__await(y_1_1.value));
112112
}
113113
}
114114
catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -144,7 +144,7 @@ function f4() {
144144
let x, y;
145145
try {
146146
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
147-
x = yield __await(y_1_1.value);
147+
x = yield yield __await(__await(y_1_1.value));
148148
}
149149
}
150150
catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -205,7 +205,7 @@ function f6() {
205205
let y;
206206
try {
207207
outer: for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
208-
const x = yield __await(y_1_1.value);
208+
const x = yield yield __await(__await(y_1_1.value));
209209
continue outer;
210210
}
211211
}

0 commit comments

Comments
 (0)