Welcome to the Linux Foundation Forum!

Output order is wrong in Stream chapter ?

Options
phantom
phantom Posts: 1
edited July 2020 in LFW211 Class Forum

order element in output is not exactly :
'use strict'
const { Readable } = require('stream')
const createReadStream = () => {
const data = ['some', 'data', 'to', 'read']
return new Readable({
encoding: 'utf8',
read () {
if (data.length === 0) this.push(null)
else this.push(data.pop())
}
})
}
const readable = createReadStream()
readable.on('data', (data) => { console.log('got data', data) })
readable.on('end', () => { console.log('finished reading') })

It should be returned:
"
got data read
got data to
got data data
got data some
finished reading
"
because data.pop() will return an element from right to left of the array.

Comments

  • davidmarkclements
    Options

    Hey @phantom well spotted, this is actually, this is an editing issue, the output is shown for this.push(data.shift()) but the code was not updated to that (and stayed as data.pop()). Thanks for letting us know!

Categories

Upcoming Training