xtodo js promise
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
- Calling an async generator function returns an AsyncGenerator object.
- These functions can use
awaitandyield(including yielding promises), combining async iteration with generators. - You rarely use the constructor directly. Prefer the
async function*syntax:
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).
- Added to the language spec in 2018
- Available in Node.js 10+
- Widely available in browsers since around January 2020
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:
- Returns a Promise
- The promise resolves to an iterator result:
{ value: any, done: boolean }
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.
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.
- async can be on arrow function
js promise
- For computer scientists.
- JS promise, the most complicated thing.
- I still don't fully understand it.
- Am relating it to call with continuation and concurrency issues.
- I need to understand and explain it in terms of loop and check.
- If you do, explain please.
- I likely won't get good answers. but i'll grade your answer, and explain the flaws.
- Alternatively, what is analogous feature in other programing language? Any?
- What is its history?
- This would also explain and put a judgement of controversy about non-blocking in nodejs 15 years ago.
- actually, gonna try asking grok.
- Basically what I'm looking for is to explain JS's promise in terms of lambda calculus, or an implementation in a basic turing complete programming language, such as basic bash or Python.
- 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.
- 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
promisesaplus
dojo Deferred