Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
async-from-sync
Advanced tools
Using async for-await-of
loop to iterate over sync generator is possible, but can cause strange effects in some edge cases (https://bugzilla.mozilla.org/show_bug.cgi?id=1610315).
This is not a problem if you are developing an app because you know what kind of iterable you have and can use the appropriate loop.
But if you are developing a library it can be nice to have the same code to work with both sync and async input. So there is a choice between a simpler code with one for-await-of
loop which works for most cases but not all and more complex code with type checking and two loops for-of
and for-await-of
.
This module lets you have one loop which just works.
// The following finally section will never be called if generator
// is consumed by for-await-of:
function* generator() {
try {
yield Promise.resolve(42)
yield Promise.reject(43)
} finally {
console.log('finally')
}
}
for await (const x of generator()) {
console.log(x)
}
// 42
// Error thrown: 43
// But if you iterate over that generator with sync for-of loop
// and await yielded promise inside that loop, then finally is called
// as expected:
for (const x of generator()) {
console.log(await x)
}
// 42
// finally
// Error thrown: 43
Let's fix that:
import { iteratorWrap } from 'async-from-sync'
function* generator() { ... }
for await (const x of iteratorWrap(generator())) {
console.log(x)
}
// 42
// finally
// Error thrown: 43
The same applies to yield*
operator if used with sync iterables.
Converts sync iterator to async one in the right way.
Returns async iterator. Does convertion if sync iterator is passed.
Does conversion only if needed. Does not any conversion if javascript engine always calls finally blocks of generators. Useful if you need to have a thing that just works:
async function F() {
for await (const x of iteratorWrap(someIterable)) { ... }
}
async function* G() {
yield* iteratorWrap(someIterable)
}
FAQs
The right way to iterate with for-await-of
The npm package async-from-sync receives a total of 2 weekly downloads. As such, async-from-sync popularity was classified as not popular.
We found that async-from-sync demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.