or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel-regenerator-runtime

Runtime library for Facebook's Regenerator, enabling ES6 generator functions and async/await to work in older JavaScript environments

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/babel-regenerator-runtime@6.5.x

To install, run

npx @tessl/cli install tessl/npm-babel-regenerator-runtime@6.5.0

index.mddocs/

babel-regenerator-runtime

babel-regenerator-runtime is the runtime library for Facebook's Regenerator that enables ES6 generator functions and ES7 async/await syntax to work in older JavaScript environments. It provides the runtime support needed for transformed generator and async functions to work properly in JavaScript environments that don't natively support these features.

Package Information

  • Package Name: babel-regenerator-runtime
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install babel-regenerator-runtime

Core Imports

The runtime is typically included automatically by Babel transforms, but can be used directly:

// Global access (standard pattern)
// The runtime is available globally as regeneratorRuntime
const runtime = regeneratorRuntime;

For module environments:

// Module access with global pollution (runtime.js)
const regeneratorRuntime = require("babel-regenerator-runtime");

For module environments without global pollution:

// Clean module access (runtime-module.js)
const regeneratorRuntime = require("babel-regenerator-runtime/runtime-module");

Basic Usage

The runtime is primarily used internally by Babel-transformed code, but can be used directly for advanced use cases:

// Used by Babel-transformed generators (automatic)
function* myGenerator() {
  yield 1;
  yield 2;
  return 3;
}

// Equivalent to how Babel transforms the generator
var _marked = regeneratorRuntime.mark(myGenerator);
function myGenerator() {
  return regeneratorRuntime.wrap(function myGenerator$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return 1;
        case 2:
          _context.next = 4;
          return 2;
        case 4:
          return _context.abrupt("return", 3);
        case 5:
        case "end":
          return _context.stop();
      }
    }
  }, _marked, this);
}

// Direct usage
const generator = myGenerator();
console.log(generator.next()); // { value: 1, done: false }
console.log(generator.next()); // { value: 2, done: false }
console.log(generator.next()); // { value: 3, done: true }

Capabilities

Generator Function Creation

Core functionality for creating and managing generator functions.

/**
 * Creates a generator object from inner function and context
 * @param innerFn - The generator function body (required)
 * @param outerFn - The outer function (optional, defaults to Generator)
 * @param self - Context object (optional)
 * @param tryLocsList - Array of try/catch/finally locations (optional, defaults to [])
 * @returns Generator object with _invoke method
 */
function wrap(innerFn, outerFn, self, tryLocsList);

/**
 * Marks a function as a generator function by setting up prototype chain
 * @param genFun - Function to mark as generator (required)
 * @returns The marked generator function (same reference)
 */
function mark(genFun);

/**
 * Checks if a function is a generator function
 * @param genFun - Function to check (required)
 * @returns Boolean indicating if function is a generator
 */
function isGeneratorFunction(genFun);

Async Function Support

Support for async/await functionality in transformed code.

/**
 * Creates async function support using AsyncIterator
 * @param innerFn - The async function body (required)
 * @param outerFn - The outer function (optional)
 * @param self - Context object (optional)
 * @param tryLocsList - Array of try/catch/finally locations (optional)
 * @returns Promise (if outerFn is not a generator) or AsyncIterator (if outerFn is a generator)
 */
function async(innerFn, outerFn, self, tryLocsList);

/**
 * Wraps a value for async await support
 * @param arg - Value to wrap (required)
 * @returns AwaitArgument instance
 */
function awrap(arg);

Iterator Utilities

Utilities for working with iterables and creating iterators.

/**
 * Creates iterator for object keys (like for...in but in reverse order)
 * @param object - Object to iterate over keys
 * @returns Iterator function that also has value and done properties
 * Note: Returns a function, not a standard iterator object
 */
function keys(object);

/**
 * Creates iterator for any iterable (arrays, strings, iterables)
 * @param iterable - Iterable object, array-like object, or undefined/null
 * @returns Iterator object with next method, or empty iterator if no values
 */
function values(iterable);

Generator Protocol

All generators created by the runtime implement the standard generator protocol.

/**
 * Generator object created by wrap() function
 */
interface Generator {
  /**
   * Advances generator to next yield point
   * @param value - Value to send into generator
   * @returns Object with value and done properties
   */
  next(value);
  
  /**
   * Throws exception into generator at current yield point
   * @param exception - Exception to throw
   * @returns Object with value and done properties, or throws
   */
  throw(exception);
  
  /**
   * Forces generator to return with given value
   * @param value - Return value
   * @returns Object with value and done: true
   */
  return(value);
  
  /**
   * Makes generator iterable (returns this)
   * @returns The generator itself
   */
  [Symbol.iterator]();
  
  /**
   * String representation of generator
   * @returns "[object Generator]"
   */
  toString();
}

Types

/**
 * Wrapper for async await arguments
 */
function AwaitArgument(arg) {
  this.arg = arg;
}

/**
 * Execution context for generators
 */
function Context(tryLocsList) {
  this.tryEntries = [{ tryLoc: "root" }];
  // Properties: prev, next, sent, done, delegate, rval
  
  // Methods:
  this.reset = function(skipTempReset) { /* resets context state, clears temp variables */ };
  this.stop = function() { /* stops generator execution, returns final value */ };
  this.dispatchException = function(exception) { /* handles exception dispatch through try blocks */ };
  this.abrupt = function(type, arg) { /* handles abrupt completion (break, continue, return, throw) */ };
  this.complete = function(record, afterLoc) { /* completes execution record, advances to next location */ };
  this.finish = function(finallyLoc) { /* handles finally block completion */ };
  this.catch = function(tryLoc) { /* handles catch block execution, returns thrown value */ };
  this.delegateYield = function(iterable, resultName, nextLoc) { /* handles yield* delegation */ };
}

/**
 * Promise-based iteration for async generators
 */
function AsyncIterator(generator) {
  this._invoke = function(method, arg) { /* unified invoke method */ };
  this.next = function(arg) { /* advances async iterator */ };
  this.throw = function(exception) { /* throws into async iterator */ };
  this.return = function(value) { /* returns from async iterator */ };
}

Constants and States

// Generator execution states (internal)
const GenStateSuspendedStart = "suspendedStart";
const GenStateSuspendedYield = "suspendedYield";
const GenStateExecuting = "executing";
const GenStateCompleted = "completed";

// Special control flow sentinel
const ContinueSentinel = {};

// Iterator symbol (cross-platform)
const iteratorSymbol = typeof Symbol === "function" && Symbol.iterator || "@@iterator";

Error Handling

The runtime throws specific errors for invalid usage:

  • "Generator is already running" - When trying to call generator while executing
  • "illegal catch attempt" - When catch called with invalid location
  • "try statement without catch or finally" - Malformed try blocks

Environment Compatibility

  • Browsers: All modern browsers and IE9+
  • Node.js: All supported versions
  • Global Safety: Use runtime-module.js for pollution-free module access
  • Integration: Designed for automatic inclusion by Babel transforms

Advanced Usage

Manual Generator Creation

// Create a generator manually
const innerFn = 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:
        return context.abrupt("return", "done");
      case 5:
      case "end":
        return context.stop();
    }
  }
};

const generator = regeneratorRuntime.wrap(innerFn, null, this);
console.log(generator.next()); // { value: "hello", done: false }
console.log(generator.next()); // { value: "world", done: false }
console.log(generator.next()); // { value: "done", done: true }

Iterator Creation

// Create iterator for object keys (note: keys() returns a function, not an object)
const obj = { a: 1, b: 2, c: 3 };
const keysIterator = regeneratorRuntime.keys(obj);
let key;
while (!(key = keysIterator()).done) {
  console.log(key.value); // "c", "b", "a" (reverse order of enumeration)
}

// Create iterator for array
const valuesIterator = regeneratorRuntime.values([1, 2, 3]);
let result = valuesIterator.next();
while (!result.done) {
  console.log(result.value); // 1, 2, 3
  result = valuesIterator.next();
}

// values() also handles strings and other iterables
const stringIterator = regeneratorRuntime.values("hello");
let char = stringIterator.next();
while (!char.done) {
  console.log(char.value); // "h", "e", "l", "l", "o"
  char = stringIterator.next();
}