← Download MyJS

MyJS block

Hidden Function Constructors 1.0.0

Gives an easy way to reference AsyncFunction, GeneratorFunction and AsyncGeneratorFunction.

Download with this
Exports
AsyncFunction GeneratorFunction AsyncGeneratorFunction
Works in
Browser and Node.js
The code of version 1.0.0
/**
 * The constructor of async functions. Unlike `Function`, it isn't a global,
 * so this is the only way to get it.
 *
 * Like `Function`, it builds a function from strings: the parameter names,
 * then the body (which can use `await`). It works with or without `new`.
 * Because the code is compiled from strings, it's blocked on pages whose
 * Content Security Policy doesn't allow `'unsafe-eval'`.
 *
 * Every async function is an instance of it, so it also tells them apart
 * from other functions: `fn instanceof AsyncFunction`.
 *
 * @function AsyncFunction
 * @param {...string} args
 *   The names of the parameters, followed by the function's body.
 * @returns {function(...*): Promise<*>}
 *   The new async function.
 *
 * @example
 * const add = new AsyncFunction('a', 'b', 'return a + await b;');
 * await add(1, Promise.resolve(2)); // 3
 *
 * @example
 * (async () => {}) instanceof AsyncFunction; // true
 * (() => {}) instanceof AsyncFunction;       // false
 */
const AsyncFunction = (async x => x).constructor;

/**
 * The constructor of generator functions (`function*`). Unlike `Function`,
 * it isn't a global, so this is the only way to get it.
 *
 * Like `Function`, it builds a function from strings: the parameter names,
 * then the body (which can use `yield`). It works with or without `new`.
 * Because the code is compiled from strings, it's blocked on pages whose
 * Content Security Policy doesn't allow `'unsafe-eval'`.
 *
 * Every generator function is an instance of it, so it also tells them apart
 * from other functions: `fn instanceof GeneratorFunction`.
 *
 * @function GeneratorFunction
 * @param {...string} args
 *   The names of the parameters, followed by the function's body.
 * @returns {function(...*): Generator}
 *   The new generator function.
 *
 * @example
 * const count = new GeneratorFunction('n', 'for (let i = 1; i <= n; i++) yield i;');
 * [...count(3)]; // [1, 2, 3]
 *
 * @example
 * (function* () {}) instanceof GeneratorFunction; // true
 * (function () {}) instanceof GeneratorFunction;  // false
 */
const GeneratorFunction = (function* () {}).constructor;

/**
 * The constructor of async generator functions (`async function*`). Unlike
 * `Function`, it isn't a global, so this is the only way to get it.
 *
 * Like `Function`, it builds a function from strings: the parameter names,
 * then the body (which can use both `await` and `yield`). It works with or
 * without `new`. Because the code is compiled from strings, it's blocked on
 * pages whose Content Security Policy doesn't allow `'unsafe-eval'`.
 *
 * Every async generator function is an instance of it, so it also tells them
 * apart from other functions: `fn instanceof AsyncGeneratorFunction`.
 *
 * @function AsyncGeneratorFunction
 * @param {...string} args
 *   The names of the parameters, followed by the function's body.
 * @returns {function(...*): AsyncGenerator}
 *   The new async generator function.
 *
 * @example
 * const ticks = new AsyncGeneratorFunction('n', `
 *   for (let i = 1; i <= n; i++) {
 *     await new Promise(resolve => setTimeout(resolve, 100));
 *     yield i;
 *   }
 * `);
 * for await (const i of ticks(3)) {
 *   console.log(i); // 1, 2, 3 (100 ms apart)
 * }
 *
 * @example
 * (async function* () {}) instanceof AsyncGeneratorFunction; // true
 * (function* () {}) instanceof AsyncGeneratorFunction;       // false
 */
const AsyncGeneratorFunction = (async function* () {}).constructor;

Versions

  1. 1.0.0 Oct 16, 2026

    Initial

JSDoc