or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

babel-helpers.mdcore-js-polyfills.mdindex.mdregenerator-runtime.md
tile.json

regenerator-runtime.mddocs/

Regenerator Runtime

Generator function and async/await support through the regenerator-runtime package, providing the runtime environment for transformed generator and async functions. This module serves as a bridge to the regenerator-runtime package.

Capabilities

Regenerator Runtime Import

Access to the regenerator runtime object that provides execution context for generators and async functions.

/**
 * Regenerator runtime object for generator and async function execution
 * @module babel-runtime/regenerator
 * @returns {Object} The regenerator runtime object
 */
import regeneratorRuntime from "babel-runtime/regenerator";

// Alternative CommonJS import
const regeneratorRuntime = require("babel-runtime/regenerator");

Note: Unlike other babel-runtime modules, the regenerator module exports the runtime directly (not wrapped with { default, __esModule }).

Runtime Methods

The regenerator runtime provides several key methods used by transformed code:

/**
 * Creates a generator function wrapper
 * @param {GeneratorFunction} fn - The original generator function
 * @returns {GeneratorFunction} Wrapped generator function with runtime support
 */
regeneratorRuntime.mark(fn);

/**
 * Wraps generator execution with proper state management
 * @param {Function} innerFn - The inner generator function
 * @param {GeneratorFunction} outerFn - The outer generator function  
 * @param {Object} self - The 'this' context
 * @param {Array} tryLocsList - Try/catch location list
 * @returns {Generator} Generator object with proper iteration protocol
 */
regeneratorRuntime.wrap(innerFn, outerFn, self, tryLocsList);

/**
 * Async function wrapper creation
 * @param {GeneratorFunction} fn - Generator representing async function
 * @param {Object} self - The 'this' context
 * @returns {Function} Function that returns a Promise
 */
regeneratorRuntime.async(fn, self);

/**
 * Await expression handling in async generators
 * @param {any} arg - Value to await
 * @returns {AwaitValue} Wrapped await value
 */
regeneratorRuntime.awrap(arg);

Usage Examples

Basic Generator Function:

// Input generator function
function* numbers() {
  yield 1;
  yield 2;
  yield 3;
}

// Transformed using regenerator runtime
import _regeneratorRuntime from "babel-runtime/regenerator";

var _marked = /*#__PURE__*/_regeneratorRuntime.mark(numbers);

function numbers() {
  return _regeneratorRuntime.wrap(function numbers$(_context) {
    while (1) switch (_context.prev = _context.next) {
      case 0:
        _context.next = 2;
        return 1;
      case 2:
        _context.next = 4;
        return 2;
      case 4:
        _context.next = 6;
        return 3;
      case 6:
      case "end":
        return _context.stop();
    }
  }, _marked, this);
}

// Usage
const gen = numbers();
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3

Async Function with Regenerator:

// Input async function
async function fetchData(url) {
  const response = await fetch(url);
  return response.json();
}

// Transformed using regenerator runtime and asyncToGenerator helper
import _regeneratorRuntime from "babel-runtime/regenerator";
import _asyncToGenerator from "babel-runtime/helpers/asyncToGenerator";

var fetchData = function () {
  var _ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(url) {
    var response;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return fetch(url);
        case 2:
          response = _context.sent;
          return _context.abrupt("return", response.json());
        case 4:
        case "end":
          return _context.stop();
      }
    }, _callee, this);
  }));

  return function fetchData(_x) {
    return _ref.apply(this, arguments);
  };
}();

Async Generator Function:

// Input async generator
async function* asyncNumbers() {
  yield await Promise.resolve(1);
  yield await Promise.resolve(2);
  yield await Promise.resolve(3);
}

// Transformed using regenerator runtime
import _regeneratorRuntime from "babel-runtime/regenerator";

var _marked = /*#__PURE__*/_regeneratorRuntime.mark(asyncNumbers);

function asyncNumbers() {
  return _regeneratorRuntime.wrap(function asyncNumbers$(_context) {
    while (1) switch (_context.prev = _context.next) {
      case 0:
        _context.next = 2;
        return _regeneratorRuntime.awrap(Promise.resolve(1));
      case 2:
        _context.next = 4;
        return _context.sent;
      case 4:
        _context.next = 6;
        return _regeneratorRuntime.awrap(Promise.resolve(2));
      case 6:
        _context.next = 8;
        return _context.sent;
      case 8:
        _context.next = 10;
        return _regeneratorRuntime.awrap(Promise.resolve(3));
      case 10:
        _context.next = 12;
        return _context.sent;
      case 12:
      case "end":
        return _context.stop();
    }
  }, _marked, this);
}

Manual Runtime Usage (not recommended):

// Direct usage of regenerator runtime
const regeneratorRuntime = require("babel-runtime/regenerator");

// Create a simple generator manually
const marked = regeneratorRuntime.mark(function* gen() {
  return regeneratorRuntime.wrap(function(context) {
    while (1) switch (context.prev = context.next) {
      case 0:
        context.next = 2;
        return "hello";
      case 2:
        context.next = 4;
        return "world";
      case 4:
      case "end":
        return context.stop();
    }
  }, gen, this);
});

const generator = marked();
console.log(generator.next().value); // "hello"
console.log(generator.next().value); // "world"

Integration with Babel Plugin

The regenerator runtime is typically used automatically by babel-plugin-transform-runtime:

// babel-plugin-transform-runtime configuration
{
  "plugins": [
    ["transform-runtime", {
      "regenerator": true,  // Enable regenerator runtime imports
      "moduleName": "babel-runtime"  // Specify runtime module name
    }]
  ]
}

When enabled, the plugin automatically:

  • Imports regenerator runtime when generators or async functions are used
  • Transforms generator functions to use regeneratorRuntime.mark and regeneratorRuntime.wrap
  • Transforms async functions to use regenerator with asyncToGenerator helper
  • Handles await expressions in async generators with regeneratorRuntime.awrap

Runtime State Management

The regenerator runtime manages execution state through context objects:

/**
 * Generator execution context
 */
interface GeneratorContext {
  prev: number;        // Previous case number
  next: number;        // Next case number to execute
  sent: any;          // Value sent via generator.next(value)
  abrupt(type: string, arg: any): any;  // Handles return/throw
  stop(): any;        // Stops generator execution
}

/**
 * Try/catch location information
 */
interface TryLocation {
  tryLoc: number;     // Start of try block
  catchLoc?: number;  // Start of catch block  
  finallyLoc?: number; // Start of finally block
  afterLoc?: number;  // Location after try/catch/finally
}

Error Handling

The regenerator runtime provides comprehensive error handling for generators and async functions:

  • Try/catch blocks: Properly handles exceptions in generator functions
  • Finally blocks: Ensures finally blocks execute even when generators are aborted
  • Promise rejection: Converts rejected promises to thrown exceptions in async functions
  • Generator return: Handles early return from generators via return statements

Performance Considerations

  • The regenerator runtime adds overhead to generator and async function execution
  • Generated code uses state machines which may be less efficient than native implementations
  • For performance-critical code, consider using native async/await in environments that support it
  • The runtime is designed for compatibility rather than maximum performance

Dependencies

This module directly re-exports from the regenerator-runtime package:

// babel-runtime/regenerator/index.js
module.exports = require("regenerator-runtime");

The actual regenerator-runtime package (^0.11.0) provides:

  • Generator function execution environment
  • Async function Promise integration
  • State machine management for complex control flow
  • Error handling and cleanup for generators

Types

// Regenerator runtime module exports directly (no __esModule wrapper)
type RegeneratorRuntimeModule = typeof regeneratorRuntime;

// Core runtime methods
interface RegeneratorRuntime {
  mark(fn: GeneratorFunction): GeneratorFunction;
  wrap(innerFn: Function, outerFn: GeneratorFunction, self?: any, tryLocsList?: TryLocation[]): Generator;
  async(fn: GeneratorFunction, self?: any): Function;
  awrap(arg: any): AwaitValue;
}

// Generator context for state management
interface GeneratorContext {
  prev: number;
  next: number;
  sent: any;
  abrupt(type: string, arg?: any): any;
  stop(): any;
}

// Async generator await wrapper
interface AwaitValue {
  value: any;
}