-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async Iteration and down-level Generators #12346
Conversation
This PR is planed on |
@mhegazy, Is there anyone else you want to review this or should I merge? |
]; | ||
|
||
var es2017LibrarySourceMap = es2017LibrarySource.map(function (source) { | ||
return { target: "lib." + source, sources: ["header.d.ts", source] }; | ||
}); | ||
|
||
var esnextLibrarySource = [ | ||
"esnext.asynciterable.d.ts" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to update VS setup to include this file as well.
src/compiler/comments.ts
Outdated
@@ -272,7 +272,7 @@ namespace ts { | |||
|
|||
function forEachLeadingCommentToEmit(pos: number, cb: (commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean, rangePos: number) => void) { | |||
// Emit the leading comments only if the container's pos doesn't match because the container should take care of emitting these comments | |||
if (containerPos === -1 || pos !== containerPos) { | |||
if ((containerPos === -1 || pos !== containerPos)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the parens are not needed
src/compiler/comments.ts
Outdated
@@ -284,7 +284,7 @@ namespace ts { | |||
|
|||
function forEachTrailingCommentToEmit(end: number, cb: (commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean) => void) { | |||
// Emit the trailing comments only if the container's end doesn't match because the container should take care of emitting these comments | |||
if (containerEnd === -1 || (end !== containerEnd && end !== declarationListContainerEnd)) { | |||
if ((containerEnd === -1 || (end !== containerEnd && end !== declarationListContainerEnd))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
src/compiler/checker.ts
Outdated
@@ -21132,7 +21287,7 @@ namespace ts { | |||
function checkExternalEmitHelpers(location: Node, helpers: ExternalEmitHelpers) { | |||
if ((requestedExternalEmitHelpers & helpers) !== helpers && compilerOptions.importHelpers) { | |||
const sourceFile = getSourceFileOfNode(location); | |||
if (isEffectiveExternalModule(sourceFile, compilerOptions)) { | |||
if (!isDeclarationFile(sourceFile) && isEffectiveExternalModule(sourceFile, compilerOptions)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you need this here, should not the new helpers only be visible in expressions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it is needed, then it should be isInAmbientContext
Just curious, why is the |
it is, just use |
This PR adds full support for Async Generators and Async Iteration (
for..await..of
) as per the current TC39 Proposal for all script targets. It also contains full support for Generators in ES5/3, includingfor..of
, spread, and iterator destructuring.Iterators
ES6 introduced
Iterator
, which is an object that exposes three methods,next
,return
, andthrow
, as per the following interface:This kind of iterator is useful for iterating over synchronously available values, such as the elements of an Array or the keys of a Map. An object that supports iteration is said to be "iterable" if it has a
Symbol.iterator
method that returns anIterator
object.Async Iterators
The Async Iteration proposal introduces an
AsyncIterator
, which is similar toIterator
. The difference lies in the fact that thenext
,return
, andthrow
methods of anAsyncIterator
return aPromise
for the iteration result, rather than the result itself. This allows the caller to enlist in an asynchronous notification for the time at which theAsyncIterator
has advanced to the point of yielding a value. AnAsyncIterator
has the following shape:An object that supports async iteration is said to be "iterable" if it has a
Symbol.asyncIterator
method that returns anAsyncIterator
object.Generators
ES6 also introduced "Generators", which are functions that can be used to yield partial computation results via the
Iterator
interface and theyield
keyword. Generators can also internally delegate calls to another iterable throughyield *
. For example:Async Generators
The Async Iteration proposal introduces "Async Generators", which are async functions that also can be used to yield partial computation results. Async Generators can also delegate calls via
yield*
to either an iterable or async iterable:As with Generators, Async Generators can only be function declarations, function expressions, or methods of classes or object literals. Arrow functions cannot be Async Generators. Async Generators require a valid, global
Promise
implementation (either native or an ES6-compatible polyfill), in addition to a validSymbol.asyncIterator
reference (either a native symbol or a shim).The
for-await-of
StatementFinally, ES6 introduced the
for..of
statement as a means of iterating over an iterable. Similarly, the Async Iteration proposal introduces thefor..await..of
statement to iterate over an async iterable:The
for..await..of
statement is only legal within an Async Function or Async Generator.Generators and Iteration for ES5/3
In addition, the following changes are included to bring full support for Generators to ES5/ES3:
-downlevelIteration
compiler option, used when targeting ES5 or ES3.[Symbol.iterator]()
method on the iterated object if it is found, and creates a synthetic array iterator over the object if it is not. This requires a nativeSymbol.iterator
orSymbol.iterator
shim at runtime for any non-array values.for..of
are only supported for arrays as is the current behavior in TypeScript 2.1 and earlier.Symbol.iterator
in ES5/3 if available when using-downlevelIteration
, but can be used on an Array even if it does not defineSymbol.iterator
at run time or design time.Symbol.iterator
in ES5/3 if available when using-downlevelIteration
, but can be used on Array even if it does not defineSymbol.iterator
at run time or design time.for..of
statements supportSymbol.iterator
in ES5/3 if available when using-downlevelIteration
, but can be used on an Array even if it does not defineSymbol.iterator
at run time or design time.Symbol.iterator
polyfill at run time to operate.Related issues