or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

array-iteration-helpers.mdasync-generator-helpers.mdclass-object-helpers.mdcore-helper-management.mdindex.mdmodule-import-helpers.mdutility-runtime-helpers.md
tile.json

async-generator-helpers.mddocs/

Async and Generator Helpers

Helper functions for async/await syntax, generator functions, and async iteration patterns including regenerator runtime integration.

Capabilities

Async Function Helpers

Core helpers for async/await syntax transformation.

/**
 * Convert async functions to generator-based implementation
 * Helper name: "asyncToGenerator"
 */
function _asyncToGenerator<T extends any[], R>(
  fn: (...args: T) => Generator<any, R, any>
): (...args: T) => Promise<R>;

/**
 * Async iterator helper for for-await-of loops
 * Helper name: "asyncIterator"
 */
function _asyncIterator<T>(iterable: AsyncIterable<T> | Iterable<T>): AsyncIterator<T>;

/**
 * Await expression in async generators
 * Helper name: "awaitAsyncGenerator"
 */
function _awaitAsyncGenerator<T>(value: T | Promise<T>): Promise<T>;

/**
 * Await value helper for async operations
 * Helper name: "AwaitValue"
 */
function _AwaitValue<T>(value: T | Promise<T>): Promise<T>;

Async Generator Helpers

Utilities for async generator functions and iteration.

/**
 * Wrap async generator functions
 * Helper name: "wrapAsyncGenerator"
 */
function _wrapAsyncGenerator<T, TReturn, TNext>(
  fn: (this: AsyncGenerator<T, TReturn, TNext>) => AsyncGenerator<T, TReturn, TNext>
): AsyncGenerator<T, TReturn, TNext>;

/**
 * Delegate to async generator
 * Helper name: "asyncGeneratorDelegate"
 */
function _asyncGeneratorDelegate<T>(
  inner: AsyncIterator<T> | Iterator<T>,
  awaitWrap: (value: any) => Promise<any>
): AsyncIterator<T>;

Generator Helpers

Core helpers for generator functions and iteration.

/**
 * Skip first generator next() call
 * Helper name: "skipFirstGeneratorNext"
 */
function _skipFirstGeneratorNext<T>(fn: () => Generator<T>): () => Generator<T>;

Regenerator Runtime Helpers

Helpers for regenerator-based async/generator polyfills.

/**
 * Main regenerator runtime helper
 * Helper name: "regenerator"
 */
const _regenerator: {
  mark<T>(fn: T): T;
  wrap<T extends any[], R>(
    fn: (...args: T) => any,
    mark?: any,
    isAsync?: boolean
  ): (...args: T) => any;
  awrap<T>(arg: T): { __await: T };
  async<T extends any[], R>(
    fn: (...args: T) => any,
    self?: any,
    tryLoc?: any,
    arg?: any,
    resolve?: (value: R) => void,
    reject?: (reason: any) => void,
    record?: any
  ): Promise<R>;
};

/**
 * Regenerator async helper
 * Helper name: "regeneratorAsync"
 */
function _regeneratorAsync<T extends any[], R>(
  fn: (...args: T) => Generator<any, R, any>,
  self?: any,
  tryLoc?: any,
  arg?: any
): Promise<R>;

/**
 * Regenerator async generator helper
 * Helper name: "regeneratorAsyncGen"
 */
function _regeneratorAsyncGen<T, TReturn, TNext>(
  fn: () => Generator<any, TReturn, TNext>
): AsyncGenerator<T, TReturn, TNext>;

/**
 * Regenerator async iterator helper
 * Helper name: "regeneratorAsyncIterator"
 */
function _regeneratorAsyncIterator<T>(
  iterable: AsyncIterable<T> | Iterable<T>
): AsyncIterator<T>;

/**
 * Regenerator define helper
 * Helper name: "regeneratorDefine"
 */
function _regeneratorDefine(
  obj: object,
  key: PropertyKey,
  descriptor: PropertyDescriptor
): object;

/**
 * Regenerator keys helper
 * Helper name: "regeneratorKeys"
 */
function _regeneratorKeys<T extends object>(obj: T): (keyof T)[];

/**
 * Regenerator values helper
 * Helper name: "regeneratorValues"
 */
function _regeneratorValues<T>(obj: ArrayLike<T> | Iterable<T>): Iterator<T>;

/**
 * Regenerator runtime main entry point
 * Helper name: "regeneratorRuntime"
 */
function _regeneratorRuntime(): {
  wrap<T extends any[], R>(
    fn: (...args: T) => any,
    mark?: any,
    isAsync?: boolean,
    outerFn?: any
  ): (...args: T) => any;
  isGeneratorFunction(fn: any): boolean;
  mark<T>(fn: T): T;
  awrap<T>(arg: T): { __await: T };
  AsyncIterator: any;
  async<T extends any[], R>(
    fn: (...args: T) => any,
    self?: any,
    tryLoc?: any,
    arg?: any,
    outerFn?: any
  ): Promise<R>;
  keys(obj: object): Iterator<string>;
  values<T>(obj: ArrayLike<T> | Iterable<T>): Iterator<T>;
};

Overload and Control Flow

Special helpers for advanced generator control flow.

/**
 * Overload yield for advanced generator patterns
 * Helper name: "OverloadYield"
 */
class _OverloadYield<T> {
  constructor(value: T, kind: string);
  v: T;
  k: string;
}

Usage Examples

// These helpers are injected by Babel when transforming async/generator syntax
import { get } from "@babel/helpers";

// For async functions: async function foo() { await bar(); }
const asyncHelper = get("asyncToGenerator");
// Babel transforms to:
// const foo = _asyncToGenerator(function* () {
//   yield bar();
// });

// For async generators: async function* gen() { yield await value; }
const asyncGenHelper = get("wrapAsyncGenerator");
const awaitGenHelper = get("awaitAsyncGenerator");
// Babel uses these to handle async generator syntax

// For for-await-of: for await (const item of asyncIterable) { }
const asyncIterHelper = get("asyncIterator");
// Babel injects async iteration logic using this helper

// Example of how Babel might use asyncToGenerator:
function originalAsync() {
  return _asyncToGenerator(function* () {
    const result = yield fetch('/api/data');
    const data = yield result.json();
    return data;
  })();
}

// For generator functions with regenerator: function* gen() { yield 1; }
const regeneratorHelper = get("regenerator");
// Babel uses regenerator runtime for generator polyfills

Helper Dependencies

Async and generator helpers have complex dependencies:

  • asyncToGenerator may depend on regenerator helpers in older environments
  • wrapAsyncGenerator depends on awaitAsyncGenerator and asyncGeneratorDelegate
  • regeneratorAsyncGen depends on regenerator and awaitAsyncGenerator
  • asyncIterator may depend on regenerator helpers for polyfills

Common Usage Patterns

import { get, getDependencies, minVersion } from "@babel/helpers";

// Check what version is needed for async/await
console.log(minVersion("asyncToGenerator")); // "7.0.0-beta.0"

// Get all async-related helpers
const asyncHelpers = [
  "asyncToGenerator", "asyncGeneratorDelegate", "asyncIterator",
  "awaitAsyncGenerator", "wrapAsyncGenerator", "regenerator",
  "regeneratorAsync", "regeneratorAsyncGen", "skipFirstGeneratorNext"
];

// Check dependencies for async transformation
const asyncDeps = getDependencies("asyncToGenerator");
console.log(asyncDeps); // Dependencies needed for async/await

// Get helper for async generator transformation
const asyncGenHelper = get("wrapAsyncGenerator");
// This includes all necessary async generator infrastructure

// For environments that need regenerator polyfills
const regeneratorHelper = get("regenerator");
// This provides full generator/async runtime support

Advanced Patterns

Async Iterator Implementation

// When Babel encounters: for await (const item of asyncIterable)
// It generates code using the asyncIterator helper:

const asyncIteratorHelper = get("asyncIterator");
// Generated code:
// const _iterator = _asyncIterator(asyncIterable);
// let _step;
// while (!(_step = await _iterator.next()).done) {
//   const item = _step.value;
//   // loop body
// }

Custom Async Generator

// For async generator functions: async function* myGen() { ... }
// Babel uses multiple helpers:

const wrapHelper = get("wrapAsyncGenerator");
const awaitHelper = get("awaitAsyncGenerator");
// Generated:
// const myGen = _wrapAsyncGenerator(function* () {
//   yield _awaitAsyncGenerator(somePromise);
// });