Skip to content

Commit af85bf6

Browse files
committed
Added back documentation
1 parent 80ce895 commit af85bf6

File tree

7 files changed

+182
-9
lines changed

7 files changed

+182
-9
lines changed

src/asyncEvery.mjs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,27 @@ import asyncFindIndex from './asyncFindIndex.mjs'
2222
* * `value`: The current value to process
2323
* * `index`: The index in the iterable. Will start from 0.
2424
* * `iterable`: The iterable on which the operation is being performed.
25-
* @param {Queue | number} queueOrConcurrency If a queue is specified it will be used to schedule the calls to
25+
* @param {Queue | number} [queueOrConcurrency] If a queue is specified it will be used to schedule the calls to
2626
* `iteratee`. If a number is specified it will be used as the concurrency of a Queue that will be created
2727
* implicitly for the same purpose. Defaults to `1`.
2828
* @returns {Promise<boolean>} A promise that will be resolved to `true` if all values pass the truth test and `false`
2929
* if a least one of them doesn't pass it. That promise will be rejected if one of the truth test throws
3030
* an exception.
3131
* @example
32+
* // example using the default concurrency of 1
33+
* import { asyncEvery, asyncSleep } from 'modern-async'
34+
*
35+
* const array = [1, 2, 3]
36+
*
37+
* const result = await asyncEvery(array, async (v) => {
38+
* // these calls will be performed sequentially
39+
* await asyncSleep(10) // waits 10ms
40+
* return v > 0
41+
* })
42+
* console.log(result) // prints true
43+
* // total processing time should be ~ 30ms
44+
* @example
45+
* // example using a set concurrency
3246
* import { asyncEvery, asyncSleep } from 'modern-async'
3347
*
3448
* const array = [1, 2, 3]
@@ -41,6 +55,19 @@ import asyncFindIndex from './asyncFindIndex.mjs'
4155
* }, 2)
4256
* console.log(result) // prints true
4357
* // total processing time should be ~ 20ms
58+
* @example
59+
* // example using infinite concurrency
60+
* import { asyncEvery, asyncSleep } from 'modern-async'
61+
*
62+
* const array = [1, 2, 3]
63+
*
64+
* const result = await asyncEvery(array, async (v) => {
65+
* // these calls will be performed in parallel
66+
* await asyncSleep(10) // waits 10ms
67+
* return v > 0
68+
* }, Number.POSITIVE_INFINITY)
69+
* console.log(result) // prints true
70+
* // total processing time should be ~ 10ms
4471
*/
4572
async function asyncEvery (iterable, iteratee, queueOrConcurrency = 1) {
4673
assert(typeof iteratee === 'function', 'iteratee must be a function')

src/asyncFilter.mjs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,25 @@ import asyncGeneratorFilter from './asyncGeneratorFilter.mjs'
1818
* * `value`: The current value to process
1919
* * `index`: The index in the iterable. Will start from 0.
2020
* * `iterable`: The iterable on which the operation is being performed.
21-
* @param {Queue | number} queueOrConcurrency If a queue is specified it will be used to schedule the calls to
21+
* @param {Queue | number} [queueOrConcurrency] If a queue is specified it will be used to schedule the calls to
2222
* `iteratee`. If a number is specified it will be used as the concurrency of a Queue that will be created
2323
* implicitly for the same purpose. Defaults to `1`.
2424
* @returns {Promise<any[]>} A promise that will be resolved with an array containing all the values that passed
2525
* the truth test. This promise will be rejected if any of the `iteratee` calls throws an exception.
2626
* @example
27+
* // example using the default concurrency of 1
28+
* import { asyncFilter, asyncSleep } from 'modern-async'
29+
*
30+
* const array = [1, 2, 3]
31+
* const result = await asyncFilter(array, async (v) => {
32+
* // these calls will be performed sequentially
33+
* await asyncSleep(10) // waits 10ms
34+
* return v % 2 === 1
35+
* })
36+
* console.log(result) // prints [1, 3]
37+
* // total processing time should be ~ 30ms
38+
* @example
39+
* // example using a set concurrency
2740
* import { asyncFilter, asyncSleep } from 'modern-async'
2841
*
2942
* const array = [1, 2, 3]
@@ -35,6 +48,18 @@ import asyncGeneratorFilter from './asyncGeneratorFilter.mjs'
3548
* }, 2)
3649
* console.log(result) // prints [1, 3]
3750
* // total processing time should be ~ 20ms
51+
* @example
52+
* // example using infinite concurrency
53+
* import { asyncFilter, asyncSleep } from 'modern-async'
54+
*
55+
* const array = [1, 2, 3]
56+
* const result = await asyncFilter(array, async (v) => {
57+
* // these calls will be performed in parallel
58+
* await asyncSleep(10) // waits 10ms
59+
* return v % 2 === 1
60+
* }, Number.POSITIVE_INFINITY)
61+
* console.log(result) // prints [1, 3]
62+
* // total processing time should be ~ 10ms
3863
*/
3964
async function asyncFilter (iterable, iteratee, queueOrConcurrency = 1) {
4065
return await asyncIterableToArray(asyncGeneratorFilter(iterable, iteratee, queueOrConcurrency))

src/asyncFind.mjs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@ import Queue from './Queue.mjs'
1919
* * `value`: The current value to process
2020
* * `index`: The index in the iterable. Will start from 0.
2121
* * `iterable`: The iterable on which the operation is being performed.
22-
* @param {Queue | number} queueOrConcurrency If a queue is specified it will be used to schedule the calls to
22+
* @param {Queue | number} [queueOrConcurrency] If a queue is specified it will be used to schedule the calls to
2323
* `iteratee`. If a number is specified it will be used as the concurrency of a Queue that will be created
2424
* implicitly for the same purpose. Defaults to `1`.
2525
* @param {boolean} [ordered] If true this function will return on the first element in the iterable
2626
* order for which `iteratee` returned true. If false it will be the first in time.
2727
* @returns {Promise<any | undefined>} A promise that will be resolved with the first found value or rejected if one of the
2828
* `iteratee` calls throws an exception before finding a value. If no value is found it will return `undefined`.
2929
* @example
30+
* // example using the default concurrency of 1
31+
* import { asyncFind, asyncSleep } from 'modern-async'
32+
*
33+
* const array = [1, 2, 3]
34+
* const result = await asyncFind(array, async (v) => {
35+
* // these calls will be performed sequentially
36+
* await asyncSleep(Math.random() * 10) // waits a random amount of time between 0ms and 10ms
37+
* return v % 2 === 1
38+
* })
39+
* console.log(result) // prints 1
40+
* @example
41+
* // example using a set concurrency
3042
* import { asyncFind, asyncSleep } from 'modern-async'
3143
*
3244
* const array = [1, 2, 3, 4, 5]
@@ -36,7 +48,18 @@ import Queue from './Queue.mjs'
3648
* await asyncSleep(Math.random() * 10) // waits a random amount of time between 0ms and 10ms
3749
* return v % 2 === 1
3850
* }, 3)
39-
* console.log(result) // prints 1
51+
* console.log(result) // prints 1 or 3, randomly
52+
* @example
53+
* // example using infinite concurrency
54+
* import { asyncFind, asyncSleep } from 'modern-async'
55+
*
56+
* const array = [1, 2, 3]
57+
* const result = await asyncFind(array, async (v) => {
58+
* // these calls will be performed in parallel
59+
* await asyncSleep(Math.random() * 10) // waits a random amount of time between 0ms and 10ms
60+
* return v % 2 === 1
61+
* }, Number.POSITIVE_INFINITY)
62+
* console.log(result) // prints 1 or 3, randomly
4063
*/
4164
async function asyncFind (iterable, iteratee, queueOrConcurrency = 1, ordered = false) {
4265
const result = (await asyncFindInternal(iterable, iteratee, queueOrConcurrency, ordered))[1]

src/asyncFindIndex.mjs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@ import Queue from './Queue.mjs'
1919
* * `value`: The current value to process
2020
* * `index`: The index in the iterable. Will start from 0.
2121
* * `iterable`: The iterable on which the operation is being performed.
22-
* @param {Queue | number} queueOrConcurrency If a queue is specified it will be used to schedule the calls to
22+
* @param {Queue | number} [queueOrConcurrency] If a queue is specified it will be used to schedule the calls to
2323
* `iteratee`. If a number is specified it will be used as the concurrency of a Queue that will be created
2424
* implicitly for the same purpose. Defaults to `1`.
2525
* @param {boolean} [ordered] If true this function will return on the first element in the iterable
2626
* order for which `iteratee` returned true. If false it will be the first in time.
2727
* @returns {Promise<number>} A promise that will be resolved with the index of the first found value or rejected if one of the
2828
* `iteratee` calls throws an exception before finding a value. If no value is found it will return `-1`.
2929
* @example
30+
* // example using the default concurrency of 1
31+
* import { asyncFindIndex, asyncSleep } from 'modern-async'
32+
*
33+
* const array = [1, 2, 3]
34+
* const result = await asyncFindIndex(array, async (v) => {
35+
* // these calls will be performed sequentially
36+
* await asyncSleep(Math.random() * 10) // waits a random amount of time between 0ms and 10ms
37+
* return v % 2 === 1
38+
* })
39+
* console.log(result) // prints 0
40+
* @example
41+
* // example using a set concurrency
3042
* import { asyncFindIndex, asyncSleep } from 'modern-async'
3143
*
3244
* const array = [1, 2, 3, 4, 5]
@@ -36,7 +48,18 @@ import Queue from './Queue.mjs'
3648
* await asyncSleep(Math.random() * 10) // waits a random amount of time between 0ms and 10ms
3749
* return v % 2 === 1
3850
* }, 3)
39-
* console.log(result) // prints 0
51+
* console.log(result) // prints 0 or 2, randomly
52+
* @example
53+
* // example using infinite concurrency
54+
* import { asyncFindIndex, asyncSleep } from 'modern-async'
55+
*
56+
* const array = [1, 2, 3]
57+
* const result = await asyncFindIndex(array, async (v) => {
58+
* // these calls will be performed in parallel
59+
* await asyncSleep(Math.random() * 10) // waits a random amount of time between 0ms and 10ms
60+
* return v % 2 === 1
61+
* }, Number.POSITIVE_INFINITY)
62+
* console.log(result) // prints 0 or 2, randomly
4063
*/
4164
async function asyncFindIndex (iterable, iteratee, queueOrConcurrency = 1, ordered = false) {
4265
const result = (await asyncFindInternal(iterable, iteratee, queueOrConcurrency, ordered))[0]

src/asyncForEach.mjs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,24 @@ import Queue from './Queue.mjs'
1616
* * `value`: The current value to process
1717
* * `index`: The index in the iterable. Will start from 0.
1818
* * `iterable`: The iterable on which the operation is being performed.
19-
* @param {Queue | number} queueOrConcurrency If a queue is specified it will be used to schedule the calls to
19+
* @param {Queue | number} [queueOrConcurrency] If a queue is specified it will be used to schedule the calls to
2020
* `iteratee`. If a number is specified it will be used as the concurrency of a Queue that will be created
2121
* implicitly for the same purpose. Defaults to `1`.
2222
* @returns {Promise} A promise that will be resolved when all the calls to `iteratee` have been done.
2323
* This promise will be rejected if any call to `iteratee` throws an exception.
2424
* @example
25+
* // example using the default concurrency of 1
26+
* import { asyncForEach, asyncSleep } from 'modern-async'
27+
*
28+
* const array = [1, 2, 3]
29+
* await asyncForEach(array, async (v) => {
30+
* // these calls will be performed sequentially
31+
* await asyncSleep(Math.random() * 10) // waits a random amount of time between 0ms and 10ms
32+
* console.log(v)
33+
* })
34+
* // prints 1, 2 and 3 in that exact order
35+
* @example
36+
* // example using a set concurrency
2537
* import { asyncForEach, asyncSleep } from 'modern-async'
2638
*
2739
* const array = [1, 2, 3]
@@ -33,6 +45,17 @@ import Queue from './Queue.mjs'
3345
* }, 2)
3446
* // prints 1, 2 and 3 in a random order (it will always print 1 or 2 before printing 3 due to
3547
* // the concurrency limit and the internal scheduling order)
48+
* @example
49+
* // example using infinite concurrency
50+
* import { asyncForEach, asyncSleep } from 'modern-async'
51+
*
52+
* const array = [1, 2, 3]
53+
* await asyncForEach(array, async (v) => {
54+
* // these calls will be performed in parallel
55+
* await asyncSleep(Math.random() * 10) // waits a random amount of time between 0ms and 10ms
56+
* console.log(v)
57+
* }, Number.POSITIVE_INFINITY)
58+
* // prints 1, 2 and 3 in a random order
3659
*/
3760
async function asyncForEach (iterable, iteratee, queueOrConcurrency = 1) {
3861
// eslint-disable-next-line no-unused-vars

src/asyncMap.mjs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,25 @@ import asyncIterableToArray from './asyncIterableToArray.mjs'
1717
* * `value`: The current value to process
1818
* * `index`: The index in the iterable. Will start from 0.
1919
* * `iterable`: The iterable on which the operation is being performed.
20-
* @param {Queue | number} queueOrConcurrency If a queue is specified it will be used to schedule the calls to
20+
* @param {Queue | number} [queueOrConcurrency] If a queue is specified it will be used to schedule the calls to
2121
* `iteratee`. If a number is specified it will be used as the concurrency of a Queue that will be created
2222
* implicitly for the same purpose. Defaults to `1`.
2323
* @returns {Promise<any[]>} A promise that will be resolved with an array containing all the mapped value,
2424
* or will be rejected if any of the calls to `iteratee` throws an exception.
2525
* @example
26+
* // example using the default concurrency of 1
27+
* import { asyncMap, asyncSleep } from 'modern-async'
28+
*
29+
* const array = [1, 2, 3]
30+
* const result = await asyncMap(array, async (v) => {
31+
* // these calls will be performed sequentially
32+
* await asyncSleep(10) // waits 10ms
33+
* return v * 2
34+
* }, 2)
35+
* console.log(result) // prints [2, 4, 6]
36+
* // total processing time should be ~ 30ms
37+
* @example
38+
* // example using a set concurrency
2639
* import { asyncMap, asyncSleep } from 'modern-async'
2740
*
2841
* const array = [1, 2, 3]
@@ -34,6 +47,18 @@ import asyncIterableToArray from './asyncIterableToArray.mjs'
3447
* }, 2)
3548
* console.log(result) // prints [2, 4, 6]
3649
* // total processing time should be ~ 20ms
50+
* @example
51+
* // example using infinite concurrency
52+
* import { asyncMap, asyncSleep } from 'modern-async'
53+
*
54+
* const array = [1, 2, 3]
55+
* const result = await asyncMap(array, async (v) => {
56+
* // these calls will be performed in parallel
57+
* await asyncSleep(10) // waits 10ms
58+
* return v * 2
59+
* }, Number.POSITIVE_INFINITY)
60+
* console.log(result) // prints [2, 4, 6]
61+
* // total processing time should be ~ 10ms
3762
*/
3863
async function asyncMap (iterable, iteratee, queueOrConcurrency = 1) {
3964
return await asyncIterableToArray(asyncGeneratorMap(iterable, iteratee, queueOrConcurrency))

src/asyncSome.mjs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,26 @@ import Queue from './Queue.mjs'
2020
* * `value`: The current value to process
2121
* * `index`: The index in the iterable. Will start from 0.
2222
* * `iterable`: The iterable on which the operation is being performed.
23-
* @param {Queue | number} queueOrConcurrency If a queue is specified it will be used to schedule the calls to
23+
* @param {Queue | number} [queueOrConcurrency] If a queue is specified it will be used to schedule the calls to
2424
* `iteratee`. If a number is specified it will be used as the concurrency of a Queue that will be created
2525
* implicitly for the same purpose. Defaults to `1`.
2626
* @returns {Promise<boolean>} A promise that will be resolved to `true` if at least one value pass the truth test and `false`
2727
* if none of them do. That promise will be rejected if one of the truth test throws an exception.
2828
* @example
29+
* // example using the default concurrency of 1
30+
* import { asyncSome, asyncSleep } from 'modern-async'
31+
*
32+
* const array = [1, 2, 3]
33+
*
34+
* const result = await asyncSome(array, async (v) => {
35+
* // these calls will be performed sequentially
36+
* await asyncSleep(10) // waits 10ms
37+
* return v % 2 === 0
38+
* })
39+
* console.log(result) // prints true
40+
* // total processing time should be ~ 30ms
41+
* @example
42+
* // example using a set concurrency
2943
* import { asyncSome, asyncSleep } from 'modern-async'
3044
*
3145
* const array = [1, 2, 3]
@@ -37,6 +51,19 @@ import Queue from './Queue.mjs'
3751
* return v % 2 === 0
3852
* }, 2)
3953
* console.log(result) // prints true
54+
* // total processing time should be ~ 20ms
55+
* @example
56+
* // example using infinite concurrency
57+
* import { asyncSome, asyncSleep } from 'modern-async'
58+
*
59+
* const array = [1, 2, 3]
60+
*
61+
* const result = await asyncSome(array, async (v) => {
62+
* // these calls will be performed in parallel
63+
* await asyncSleep(10) // waits 10ms
64+
* return v % 2 === 0
65+
* }, Number.POSITIVE_INFINITY)
66+
* console.log(result) // prints true
4067
* // total processing time should be ~ 10ms
4168
*/
4269
async function asyncSome (iterable, iteratee, queueOrConcurrency = 1) {

0 commit comments

Comments
 (0)