Skip to content

Commit 9483ba2

Browse files
author
baochau.dinh
committed
js-concepts: parallel programming - Symbol.interator/Symbol.asyncIterator
1 parent fd31b88 commit 9483ba2

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

Parallel/README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,67 @@
11
- `Promise.race()` lets you know as soon as one of the given promises either fulfills or rejects
22
- `Promise.allSettled()` lets you know when all promises either fulfill or reject
33
- `Promise.all()` lets you know as soon as one of the given promises rejects or when all of them fulfill
4-
- `Promises.any()` lets you know as soon as one of the given promises fulfills or when none of them fulfills.
4+
- `Promises.any()` lets you know as soon as one of the given promises fulfills or when none of them fulfills.
5+
6+
## Creating custom iterator
7+
8+
`[Symbol.iterator]()` works like any other method except that it's automatically called if we use `for...of`
9+
on the object.
10+
11+
The iterator object is designed to maintain internal pointer to a position in the items.
12+
13+
```JS
14+
const collection = {
15+
a: 10,
16+
b: 20,
17+
c: 30,
18+
[Symbol.iterator]() {
19+
let i = 0;
20+
const values = Object.keys(this);
21+
return {
22+
next: () => {
23+
return {
24+
value: this[values[i++]],
25+
done: i > values.length
26+
}
27+
}
28+
}
29+
}
30+
}
31+
32+
for (const value of collection) {
33+
console.log(value);
34+
}
35+
```
36+
37+
## Creating custom asynchronous iterator
38+
39+
An object is classified as asychronous iterable when it has a `Symbol.asyncIterator` method that returns an
40+
asynchronous iterator.
41+
42+
```JS
43+
const collection = {
44+
a: 10,
45+
b: 20,
46+
c: 30,
47+
[Symbol.asyncIterator]() {
48+
const keys = Object.keys(this);
49+
console.log('keys: ', keys);
50+
let i = 0;
51+
return {
52+
next: () => {
53+
return new Promise((resolve, reject) => {
54+
setTimeout(() => {
55+
resolve({
56+
value: this[keys[i++]],
57+
done: i > keys.length
58+
});
59+
}, 1000);
60+
});
61+
}
62+
};
63+
}
64+
};
65+
66+
const iterator = collection[Symbol.asyncIterator]();
67+
```

Parallel/iteratorsV2.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const collection = {
2+
a: 10,
3+
b: 20,
4+
c: 30,
5+
[Symbol.iterator]() {
6+
let i = 0;
7+
const values = Object.keys(this);
8+
return {
9+
next: () => {
10+
return {
11+
value: this[values[i++]],
12+
done: i > values.length
13+
}
14+
}
15+
}
16+
}
17+
}
18+
19+
for (const value of collection) {
20+
console.log(value);
21+
}

Parallel/workWithMultipleApis.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Retrieving URLs separately
3+
*/
4+
5+
import fetch from 'node-fetch';
6+
7+
const srcArr = [
8+
'https://eloux.com/async_js/examples/1.json',
9+
'https://eloux.com/async_js/examples/2.json',
10+
'https://eloux.com/async_js/examples/3.json',
11+
];
12+
13+
srcArr[Symbol.asyncIterator] = function() {
14+
let i = 0;
15+
return {
16+
async next() {
17+
if (i === srcArr.length) {
18+
return {
19+
done: true
20+
}
21+
} else {
22+
const url = srcArr[i++];
23+
const response = await fetch(url);
24+
if (!response.ok) {
25+
throw new Error('Unable to retrieve URL: ' + url);
26+
}
27+
28+
return {
29+
value: await response.json(),
30+
done: false
31+
}
32+
}
33+
}
34+
}
35+
}
36+
37+
const iterator = srcArr[Symbol.asyncIterator]();
38+
39+
iterator.next().then(result => {
40+
console.log(result.value.firstName);
41+
});
42+
43+
iterator.next().then(result => {
44+
console.log(result.value.firstName);
45+
});
46+
47+
iterator.next().then(result => {
48+
console.log(result.value.firstName);
49+
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
"dependencies": {
2828
"async": "^3.2.3",
2929
"node-fetch": "^3.2.0"
30-
}
30+
},
31+
"type": "module"
3132
}

0 commit comments

Comments
 (0)