A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/rpetrich/babel-plugin-transform-async-to-promises below:

rpetrich/babel-plugin-transform-async-to-promises: Transform async/await to somewhat idiomatic JavaScript promise chains

babel-plugin-transform-async-to-promises

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);
	});
});
Output with hoist enabled:
function _response$blob(response) {
	return _await(response.blob(), URL.createObjectURL);
}
const fetchAsObjectURL = _async(function(url) {
	return _await(fetch(url), _response$blob);
});
Output with 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);
	}
}
Output with 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;
JavaScript Language Features

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4