Skip to content

Commit

Permalink
Prepare templates for short circuiting async functions.
Browse files Browse the repository at this point in the history
Today:
async function f() {
  var x = 10;
  return x;
}

Is transpiled to:
function f() {
  return $jscomp.executeAsyncGenerator(
      function $jscomp$async$generator() {
        var x;
        return $jscomp.generator.createGenerator(
            $jscomp$async$generator,
            function($jscomp$generator$context) {
              x = 10;
              return $jscomp$generator$context.return(x);
            });
      });
};

Will be transpiled to:
function f() {
  var x;
  return $jscomp.generator.asyncExecutePromiseGenerator(
      function ($jscomp$generator$context) {
        x = 10;
        return $jscomp$generator$context.return(x);
      });
};

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=193372804
  • Loading branch information
SergejSalnikov authored and blickly committed Apr 19, 2018
1 parent fb40101 commit b364e72
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/com/google/javascript/jscomp/js/es6/execute_async_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

'require base';
'require es6/promise';
'require es6/generator_engine';

/**
* Handles the execution of an async function.
Expand Down Expand Up @@ -98,3 +99,19 @@ $jscomp.asyncExecutePromiseGenerator = function(generator) {
$jscomp.asyncExecutePromiseGeneratorFunction = function(generatorFunction) {
return $jscomp.asyncExecutePromiseGenerator(generatorFunction());
};

/**
* Handles the execution of a state machine program that represents transpiled
* async function.
*
* @final
* @param {function(!$jscomp.generator.Context<?>): (void|{value: ?})} program
* @return {!Promise<?>}
* @suppress {reportUnknownTypes, visibility}
*/
$jscomp.asyncExecutePromiseGeneratorProgram = function(program) {
return $jscomp.asyncExecutePromiseGenerator(
new $jscomp.generator.Generator_(
new $jscomp.generator.Engine_(
program)));
};
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/resources.json

Large diffs are not rendered by default.

0 comments on commit b364e72

Please sign in to comment.