function* generateNumbers() {
try {
yield 1;
yield 2;
yield 3;
} finally {
console.log("Cleaning up");
}
}
function doSomething() {
using numbers = generateNumbers();
const res1 = numbers.next();
// Not iterating the rest of the numbers
// Before the function exits, the async iterator is disposed
// Logs "Cleaning up"
}
doSomething();Iterator.prototype[Symbol.dispose]() / notebook
Iterator.prototype[Symbol.dispose]()
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.