Transform async/await to somewhat idiomatic JavaScript promise chains
TypeScript
248
518 commits
updated Mar 4, 2023
Babel plugin to transform async functions containing await expressions to the equivalent chain of Promise calls with use of minimal helper functions.
async function fetchAsObjectURL(url) {
const response = await fetch(url);
const blob = await response.blob();
return URL.createObjectURL(blob);
}
const fetchAsObjectURL = _async(function(url) {
return _await(fetch(url), function(response) {
return _await(response.blob(), URL.createObjectURL);
});
});
hoist enabled:function _response$blob(response) {
return _await(response.blob(), URL.createObjectURL);
}
const fetchAsObjectURL = _async(function(url) {
return _await(fetch(url), _response$blob);
});
inlineHelpers enabled:const fetchAsObjectURL = function(url) {
try {
return Promise.resolve(fetch(url)).then(function(response) {
return Promise.resolve(response.blob()).then(URL.createObjectURL);
});
} catch(e) {
return Promise.reject(e);
}
}
externalHelpers enabled:In the normal case, helpers are added to the top of the file for the _async and _await functions (as well as others). This can cause bloat in a codebase due to duplication of helper code in every file. To avoid this, enable externalHelpers and those will be imported instead:
import { _async } from "babel-plugin-transform-async-to-promises/helpers";
import { _await } from "babel-plugin-transform-async-to-promises/helpers";
const fetchAsObjectURL = _async(function(url) {
return _await(fetch(url), function(response) {
return _await(response.blob(), URL.createObjectURL);
});
});
export default fetchAsObjectURL;
async/awaitfor/while/do loops (including loops that would exhaust stack if dispatched recursively)switch statements (including fallthrough and default cases)try/catchbreak/continue statements (on both loops and labeled statements)throw expressionsargumentsthisFunction.length: async functions will often return a length of 0 (when the _async wrapper is used)topLevelAwait option to return when using SystemJS.eval: impossible to support without deep hooks into the runtimeFunction.name: rewrite pass removes function name instrumentationnew AsyncFunction(...): impossible to support without shipping babel and the plugin in the outputTypeScript
64.8%
JavaScript
35.2%
Transform async/await to somewhat idiomatic JavaScript promise chains
TypeScript
248
518 commits
updated Mar 4, 2023
Babel plugin to transform async functions containing await expressions to the equivalent chain of Promise calls with use of minimal helper functions.
async function fetchAsObjectURL(url) {
const response = await fetch(url);
const blob = await response.blob();
return URL.createObjectURL(blob);
}
const fetchAsObjectURL = _async(function(url) {
return _await(fetch(url), function(response) {
return _await(response.blob(), URL.createObjectURL);
});
});
hoist enabled:function _response$blob(response) {
return _await(response.blob(), URL.createObjectURL);
}
const fetchAsObjectURL = _async(function(url) {
return _await(fetch(url), _response$blob);
});
inlineHelpers enabled:const fetchAsObjectURL = function(url) {
try {
return Promise.resolve(fetch(url)).then(function(response) {
return Promise.resolve(response.blob()).then(URL.createObjectURL);
});
} catch(e) {
return Promise.reject(e);
}
}
externalHelpers enabled:In the normal case, helpers are added to the top of the file for the _async and _await functions (as well as others). This can cause bloat in a codebase due to duplication of helper code in every file. To avoid this, enable externalHelpers and those will be imported instead:
import { _async } from "babel-plugin-transform-async-to-promises/helpers";
import { _await } from "babel-plugin-transform-async-to-promises/helpers";
const fetchAsObjectURL = _async(function(url) {
return _await(fetch(url), function(response) {
return _await(response.blob(), URL.createObjectURL);
});
});
export default fetchAsObjectURL;
async/awaitfor/while/do loops (including loops that would exhaust stack if dispatched recursively)switch statements (including fallthrough and default cases)try/catchbreak/continue statements (on both loops and labeled statements)throw expressionsargumentsthisFunction.length: async functions will often return a length of 0 (when the _async wrapper is used)topLevelAwait option to return when using SystemJS.eval: impossible to support without deep hooks into the runtimeFunction.name: rewrite pass removes function name instrumentationnew AsyncFunction(...): impossible to support without shipping babel and the plugin in the outputTypeScript
64.8%
JavaScript
35.2%