Skip to content

Instantly share code, notes, and snippets.

@marsicdev
Created October 27, 2022 13:47
Show Gist options
  • Save marsicdev/5c3b4529458206fb28110f54b6974777 to your computer and use it in GitHub Desktop.
Save marsicdev/5c3b4529458206fb28110f54b6974777 to your computer and use it in GitHub Desktop.
Code samples form ReactWeek TechTalk #5 - Advanced JavaScript features you might need https://speakerdeck.com/marsicdev/techtalk-advanced-javascript-concepts-you-might-need
/**********************************************/
/* How to use AbortController with the Fetch API
/**********************************************/
const url = 'https://jsonplaceholder.typicode.com/todos/1'
const abortController1 = new AbortController()
const abortSignal1 = abortController1.signal
const fetchTodo = async () => {
try {
const response = await fetch(url, { signal: abortSignal1 })
const todo = await response.json()
console.log(todo)
} catch (error) {
if (error.name === 'AbortError') {
console.log('Operation timed out')
} else {
console.error(error)
}
}
}
fetchTodo()
abortController1.abort()
/**********************************************/
/* How to use AbortController with fs.readFile
/**********************************************/
const fs = require('node:fs')
const abortFRcontroller = new AbortController()
const { signal } = abortFRcontroller
fs.readFile('data.txt', { signal, encoding: 'utf8' }, (error, data) => {
if (error) {
if (error.name === 'AbortError') {
console.log('Read file process aborted')
} else {
console.error(error)
}
return
}
console.log(data)
})
controller.abort()
/**********************************************/
/* Generator Functions
/**********************************************/
function* generatorFunction() {}
function* generatorFunction() {}
// Generator function expression
const generatorFunction = function* () {}
// Generator as the method of an object
const generatorObj = {
*generatorMethod() {},
}
// Generator as the method of a class
class GeneratorClass {
*generatorMethod() {}
}
/**********************************************/
/* yield Operators
/**********************************************/
// Create a generator function with multiple yields
function* matrixGenerator() {
yield 'Neo'
yield 'Morpheus'
yield 'Trinity'
return 'The Oracle'
}
const matrixGenerator = matrixGenerator()
// Call next four times
matrixGenerator.next()
matrixGenerator.next()
matrixGenerator.return()
matrixGenerator.next()
/**********************************************/
/* Iterating Over a Generator
/**********************************************/
// Iterate over Generator object
for (const value of generator) {
console.log(value)
}
// Create an array from the values of a Generator object
const values = [...generator]
console.log(values)
/**********************************************/
/* Closing a Generator
/**********************************************/
function* generatorFunction() {
yield 'Neo'
yield 'Morpheus'
yield 'Trinity'
}
const generator1 = generatorFunction()
generator.next()
generator.return('There is no spoon!')
generator.next()
/**********************************************/
/* try catch
/**********************************************/
// Define a generator function with a try...catch
function* generatorFunction() {
try {
yield 'Neo'
yield 'Morpheus'
} catch (error) {
console.log(error)
}
}
// Invoke the generator and throw an error
const generator3 = generatorFunction()
/**********************************************/
/* yield Delegation
/**********************************************/
// Generator function that will be delegated to
function* numDelegate() {
yield 3
yield 4
}
// Outer generator function
function* mainGen() {
yield 1
yield 2
yield* numDelegate()
}
// Iterate through the outer generator
const generator4 = mainGen()
for (const value of generator4) {
console.log(value)
}
/**********************************************/
/* Passing Values in Generators
/**********************************************/
function* generatorFunction() {
console.log(yield)
console.log(yield)
return 'The end'
}
const generator5 = generatorFunction()
generator5.next()
generator5.next(100)
generator5.next(200)
/**********************************************/
/* async/await with Generators
/**********************************************/
const getUsers = async function () {
const response = await fetch('https://jsonplaceholder.typicode.com/users')
const json = await response.json()
return json
}
// Call the getUsers function and log the response
getUsers().then((response) => console.log(response))
const getUsersGen = asyncAlt(function* () {
const response = yield fetch('https://jsonplaceholder.typicode.com/users')
const json = yield response.json()
return json
})
// Invoking the function
getUsersGen().then((response) => console.log(response))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment