Slide 17
Slide 17 text
4USFBNͱϑϩʔ੍ޚ૬ੑ͕ѱ͍
͜Ε·Ͱ4USFBNΛϑϩʔ੍ޚʹΈࠐΉ߹ɺ1SPNJTF
ͰแΈࠐΈϫϯγϣοτͷΠϯλʔϑΣʔεʹม͢Δ͔ɺ
ಠࣗʹΠςϨʔγϣϯରԠͤ͞Δͱ͍͏ঢ়گͩͬͨɻ
const recv = new Promise((resolve, reject) => {
let data = ''
stream.on('data', (chunk) => {
data += chunk
})
stream.once('end', () => {
return resolve(data)
})
stream.once(‘error', (err) => {
return reject(err)
})
})
const data = await recv()
const drain = (stream) => {
return new Promise((resolve, reject) => {
stream.once('data', (data) => {
stream.pause()
…
resolve(data)
})
stream.once('end', () => {
…
resolve(null)
})
stream.once('error', (err) => {
…
reject(err)
})
stream.resume()
})
}
const main = async () => {
const stream = fs.createReadStream(‘file’)
let data = null
while (data = await drain(stream)) {
console.log(data)
}
}
1SPNJTFͰแΈࠐΉ
ϫϯγϣοτม
ෳࡶղͳ
ΠςϨʔγϣϯରԠ