xtodo js promise

work in progress
work in progress

what's AsyncGeneratorFunction

what's AsyncGeneratorFunction

(ai answer)

AsyncGeneratorFunction is the built-in constructor for async generator functions in JavaScript (functions declared with async function*).

Key points

async function* myAsyncGen() {
  yield 1;
  yield await Promise.resolve(2);
}

Accessing the constructor

const AsyncGeneratorFunction = Object.getPrototypeOf(async function*(){}).constructor;

const genFunc = new AsyncGeneratorFunction('a', 'yield a;');

It is part of the standard ECMAScript built-ins alongside Function, GeneratorFunction, and AsyncFunction.

when is it added

ES2018 (ECMAScript 2018).

It came with the Asynchronous Iteration proposal (async generators + for await...of).

explain AsyncIterator protocol

AsyncIterator protocol is the async counterpart to the regular Iterator protocol. It defines how objects produce a sequence of values asynchronously.

Requirements

An object is an async iterator if it has a next() method that:

Optional methods: return() and throw() (also return Promises).

Async iterable

An object is an async iterable if it has a Symbol.asyncIterator method that returns an async iterator.

Usage

for await (const value of asyncIterable) {
  console.log(value);
}

This is how you consume async iterators. Async generators (async function*) automatically implement the protocol and produce AsyncGenerator objects.

Summary:
next() → Promise<{value, done}>
Symbol.asyncIterator → async iterator
Introduced in ES2018 alongside async generators.


work in progress

in JavaScript, does await always return a promise

(ai answer)

No. await does not return a Promise.

The await expression waits for a Promise (or thenable) to settle and yields its fulfillment value. If the awaited expression is not a thenable, it simply yields that value directly.

async function example() {
  const value = await Promise.resolve(42);
  console.log(value); // 42 (a number, not a Promise)

  const plain = await 100;
  console.log(plain); // 100
}

What does always return a Promise is an async function itself—even if it returns a plain value, that value is automatically wrapped in a resolved Promise.

This puzzled me for a decade. Node js non-blocking/async 2012, fad or progress? In fact, most programer don't know it before node js made it popular in around 2010. But it exists in programing languages, in niche tasks of io/events/threads coding.

work in progress

js promise

js promise 2025-05-24 31d5b
js promise 2025-05-24 31d5b
js promise 2025-05-24 314bd
js promise 2025-05-24 314bd
js promise 2025-05-24 313b0
js promise 2025-05-24 313b0
  • The history goes back to MochiKit implementing Twisted (a Python library) Deferreds in JS.
  • Dojo Toolkit borrowed the concept from MochiKit (I think MochiKit's author donated the code to Dojo).
  • Promises ended up being a subset of Deferred functionality.
  • Links to follow:
  • Also, @kriszyp, @slightlylate, @phiggins, @dylans, @ttrenka, and many others I can't remember were instrumental in getting Deferreds and Promises into JS 20+ years ago.
  • It was an honor working with them back then.

js promise 2025-05-24 317cf
js promise 2025-05-24 317cf
  • Manuel Simoni
  • @msimoni
  • Fundamentally, it's a way to make callbacks (which is the only way you can do IO in JS) more palatable, by wrapping them in a convenient object.
  • There's quite a bit going on; look into a pure-JS implementation of the "Promises/A+" spec such as this one:
  • https://github.com/abdulapopoola/Adehun/blob/master/adehun.js
js promise adehun.js 2025-05-24 31835
js promise adehun.js 2025-05-24 31835

promisesaplus

promisesaplus 2025-05-24 2337b
promisesaplus 2025-05-24 2337b

dojo Deferred

dojo deferred 2025-05-24 3145f
dojo deferred 2025-05-24 3145f https://dojotoolkit.org/reference-guide/1.10/dojo/Deferred.html