Skip to content

Instantly share code, notes, and snippets.

@krambertech
Last active November 19, 2016 08:25
Show Gist options
  • Save krambertech/5b968206c3f04d1b5c7cb2d817f666bb to your computer and use it in GitHub Desktop.
Save krambertech/5b968206c3f04d1b5c7cb2d817f666bb to your computer and use it in GitHub Desktop.
ES6 Symbol / For-of / Iterator / Generator
// --- //
const secret = Symbol();
const user = {
id: 1,
login: 'Vasya',
[secret]: '45sdfgh6n'
};
console.log('user', user);
console.log('user.login', user.login);
console.log('user.secret', user.secret);
console.log('user[secret]', user[secret]);
// --- //
const secret = Symbol('secret');
const user = {
id: 1,
login: 'Vasya',
[secret]: '45sdfgh6n'
};
console.log('user', user);
console.log('user.login', user.login);
console.log('user.secret', user.secret);
console.log('user[secret]', user[secret]);
// --- //
const symbol1 = Symbol();
const symbol2 = Symbol('foo');
const symbol3 = Symbol('foo');
console.log('symbol1', symbol1);
console.log('symbol2 == symbol3', symbol2 == symbol3);
console.log('symbol2 === symbol3', symbol2 === symbol3);
console.log('typeof symbol2', typeof symbol2);
const arr = [1, 2, 3]
for (let i = 0; i < arr.length; i++) {
console.log(arr[i])
}
// --- //
arr.forEach(function(value) {
console.log(value)
})
arr.forEach(value => console.log(value))
// --- //
for (let i in arr) {
console.log(arr[i])
}
// --- //
for (let value of arr) {
console.log(value)
}
// --- //
for (let char of 'Hello') {
console.log(char)
}
// --- //
let types = new Set(['foo', 'bar', 'foo', 'baz'])
for (let type of types) {
console.log(type)
}
// --- //
let obj = { foo: 'bar', baz: 'aux' }
for (let type of obj) {
console.log(type)
}
// --- //
let obj = { foo: 'bar', baz: 'aux' }
for (let key of obj.keys()) {
console.log(obj[key])
}
// Array.prototype[Symbol.iterator] -> function(){...}
// Set.prototype[Symbol.iterator] -> function(){...}
let iterator = {
[Symbol.iterator]: function() {
return this;
},
next: function() {
return { done: false, value: 'hey' };
}
}
// --- //
function createIterator(arr){
let index = 0;
return {
next: () => {
return index < arr.length
? { value: arr[index++], done: false }
: { done: true };
}
}
}
const myIterator = createIterator(['hey', 'yo', 'ya']);
console.log( myIterator.next() );
console.log( myIterator.next() );
console.log( myIterator.next() );
console.log( myIterator.next() );
console.log( myIterator.next() );
function* countToThree() {
yield 1;
yield 2;
return 3;
}
const counter = countToThree();
console.log( counter.next() );
console.log( counter.next() );
console.log( counter.next() );
// --- //
function* createCounter(start, end) {
for (let i = start; i <= end; i++) {
yield i;
}
}
const counter = createCounter(1, 3);
console.log( counter.next() );
console.log( counter.next() );
console.log( counter.next() );
// --- //
function* counterOfCounters() {
yield* createCounter(1, 3);
yield* createCounter(6, 9);
yield* createCounter(20, 22);
}
for (let count of counterOfCounters()) {
console.log(count);
}
// --- //
function* fetchPost() {
const request = yield fetch(`https://jsonplaceholder.typicode.com/posts/1`);
const post = yield request.json();
return post.body;
}
const iterator = fetchPost();
const promise = iterator.next().value;
promise.then(data => {
const anotherPromise = iterator.next(data).value;
anotherPromise.then(anotherData => {
console.log(iterator.next(anotherData).value);
})
})
// --- //
run(
function* getHouseMembers(id) {
const request = yield fetch(`http://anapioficeandfire.com/api/houses/${id}`);
const { swornMembers: swornMemberURLs } = yield request.json();
const swornMembers = [];
for (let swornMemberURL of swornMemberURLs) {
const swornMember = yield fetch(swornMemberURL);
const swornMemberJson = yield swornMember.json();
swornMembers.push(swornMemberJson);
}
return swornMembers;
}
, 378).then(console.log).catch(console.error);
function run(generator, ...args) {
const iterator = generator(...args);
function iterate({ done, value }) {
if (done) {
return value;
}
return value.then(data => iterate(iterator.next(data)))
}
return iterate(iterator.next());
}
// --- //
function* fetchGenerator(url) {
const request = yield fetch(url);
const json = yield request.json();
return json;
}
run(
function* getHouseMembers(id) {
const { swornMembers: swornMemberURLs } = yield* fetchGenerator(`http://anapioficeandfire.com/api/houses/${id}`);
const swornMembers = [];
for (let swornMemberURL of swornMemberURLs) {
swornMembers.push(yield* fetchGenerator(swornMemberURL));
}
return swornMembers;
}
, 378).then(console.log).catch(console.error);
function run(generator, ...args) {
const iterator = generator(...args);
function iterate({ done, value }) {
if (done) {
return value;
}
return value.then(data => iterate(iterator.next(data)))
}
return iterate(iterator.next());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment