-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation needs better examples #1609
Comments
I agree, we need much better docs. I'd also like to change many of the examples (or add mroe examples) to use async/await. However, this is something that takes time I don't have at the moment. |
Maybe I can help. |
How can I help with examples? |
There are |
note for that particular example, you could do await async.map(['file1','file2','file3'], fs.promises.stat);
// or without async lib
await Promise.all(['file1','file2','file3'].map(name => fs.promises.stat(name)) or if you want an object as result await async.auto({
file1: async()=>fs.promises.stat('file1'),
file2: async()=>fs.promises.stat('file2')
file3: async()=>fs.promises.stat('file3')
})
// or without async lib
Object.fromEntries(
await Promise.all(['file1','file2','file3'].map(async name => [name, await fs.promises.stat(name)]))
) |
I'm in particular looking for a (simple) Promise-based example of async.mapLimit, even without async/await. Haven't found any non-callback example so far at all. |
@flatcoding https://github.com/caub/misc/blob/master/utils/promise-concurrent.js, if you don't want |
Hi Cyril @caub, |
I thought by "simple Promise-based example", you meant an example without any library if you wanted an example with this library, I'm sure there are plenty |
Right, so I'm fine in general with using Promises, and also I understand how to use the 'async' modules in callback style. As documentation states, without callbacks supplied it would return a Promise. So far, I couldn't fine examples, not for async.map(Limit) at least. As this thread is about 'better examples' in the documentation, I chimed in on this here... |
Hi @caub, took a shot at improving the docs for collection methods, adding use cases for callbacks, Promises, and async/await. |
@romanbalayan nice the methods we use the most are note: I think you could avoid the async IIFE |
@caub, updated pull request to remove IIFE. (Was using it to test locally if samples were actually working) Yes, I would also like to cover the flow control soon. I was actually just waiting for feedback if the changes I made for the collection examples matches the expectations here. |
@romanbalayan what you could do is set up that PR branch as github page (go in your fork of async, settings > Github pages > Source: (pick your PR branch)) so we can preview it in http://romanbalayan.github.io/async/v3/ |
@caub, great idea. Did that, and should be available now on https://romanbalayan.github.io/async/v3/ |
Hi @caub, Added Promise and async/await samples for flow control methods - |
@romanbalayan nice! https://romanbalayan.github.io/async/v3/docs.html#auto I feel like the callback and promises examples are always quite similar, probably worth having just one of them, since anyway when using promises, we'd rather go for async/await, and it'd lighten up stuff Good stuff else! |
Should I also remove the Promises usage example on the Collections methods as well? |
I think yes you could Also for the flow control methods, you could add examples with async inner functions, example: const delay = (t, v) => new Promise(r => setTimeout(r, t, v));
try {
console.time('parallel')
const results = await async.parallel({
one: async () => delay(200, 1),
two: async () => delay(100, 2),
});
console.timeLog('parallel', results);
// parallel: 200ms { one: 1, two: 2 }
}
catch (err) {
console.log(err);
} |
@romanbalayan great work. I wish I had found your version of the documentation some days ago! I did find out how things work by trial and error after all, but it would've been less painful with the new examples :-) |
const results = await async.parallel({
one: async () => delay(200, 1),
two: async () => delay(100, 2),
}); would be natively written: const results = Object.fromEntries(
await Promise.all(
Object.entries({
one: async () => delay(200, 1),
two: async () => delay(100, 2),
})
.map(async ([k, v]) => [k, await v()])
)
); |
@caub Can i use async await inside parallel tasks ? something like below
|
Yes |
Consider this example for map
This basically tells me nothing except syntax. In most cases people aren't going to pass
fs.stat
. The examples should include use of all possible options. All examples for all functions and methods need to be updated. They are too simplisticThe text was updated successfully, but these errors were encountered: