Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cggaurav
Forked from grimen/generators-test.js
Created August 25, 2018 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cggaurav/4e872266d72e8ce04db1460e44f7f214 to your computer and use it in GitHub Desktop.
Save cggaurav/4e872266d72e8ce04db1460e44f7f214 to your computer and use it in GitHub Desktop.
Experiments with Node.js Generator API - partially requires Node 10.
/* =========================================
IMPORTS
-------------------------------------- */
const fs = require('fs')
const { promisify } = require('util')
sleep = promisify(setTimeout)
/* =========================================
BOOTSTRAP
-------------------------------------- */
let lines = ''
for (var i = 0; i < 10000; i++) {
lines += `Lorem ipsum ${i}\n`
}
filepath = '/tmp/lorem.txt'
fs.writeFileSync(filepath, lines, 'utf8')
/* =========================================
GENERATORS
-------------------------------------- */
function* loremSync(size = 10) {
for (let i = 0; i < size; i++) {
yield i
}
}
async function* loremAsync(size = 10, delay = 100) {
for (let i = 0; i < size; i++) {
await sleep(delay)
yield i
}
}
/* =========================================
USAGE: generators
-------------------------------------- */
function generatorSync(size = 10) {
console.log('\n=================================================')
console.log(' GENERATORS 1/2: Sync')
console.log('----------------------------------------------')
for (const value of loremSync(size)) {
console.log(value)
}
console.log('..............................................\n')
}
async function generatorAsync(size = 10) {
console.log('\n=================================================')
console.log(' GENERATORS 2/2: Async')
console.log('----------------------------------------------')
for await (const value of loremAsync(size)) {
console.log(value)
}
console.log('..............................................\n')
}
/* =========================================
USAGE: streams
-------------------------------------- */
function streamAsyncCallback () {
console.log('\n=================================================')
console.log(' STREAMS 1/2: Callback')
console.log('----------------------------------------------')
const stream = fs.createReadStream(filepath, {
encoding: 'utf8', // null -> buffers, 'utf8' -> strings with that encoding
highWaterMark: 1024, // maximum size of each chunk (buffer or string)
})
stream.on('data', (chunk) => {
process.stdout.write('<DATA>')
process.stdout.write(chunk)
})
stream.on('end', async () => {
console.log('..............................................\n')
// II
streamAsyncAwaitGenerator()
})
}
async function streamAsyncAwaitGenerator () {
console.log('\n=================================================')
console.log(' STREAMS 2/2: Async Generator')
console.log('----------------------------------------------')
const stream = fs.createReadStream(filepath, {
encoding: 'utf8', // null -> buffers, 'utf8' -> strings with that encoding
highWaterMark: 1024 // maximum size of each chunk (buffer or string)
})
for await (const chunk of stream) {
process.stdout.write('<DATA>')
process.stdout.write(chunk)
}
console.log('..............................................\n')
// III + IV
generatorSync()
generatorAsync()
}
/* =========================================
MAIN
-------------------------------------- */
// I
streamAsyncCallback() // NOTE: starts callback chain
// streamAsyncAwaitGenerator()
// generatorSync()
// generatorAsync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment