Skip to content

Instantly share code, notes, and snippets.

View matianfu's full-sized avatar
🤣

Tianfu Ma matianfu

🤣
  • Shanghai Dingnan Intellitech
  • Shanghai, China
View GitHub Profile
@matianfu
matianfu / concurrent.js
Last active July 24, 2017 10:04
A base class for writing concurrent processes, with abort, until, race and settle.
// licensed under public domain
// author: matianfu@gmail.com
const EventEmitter = require('events')
// K combinator, not necessary, just for fun
const K = x => y => x
// this class is mainly for settle logic.
// the concrete class should emit a 'finish' event with err/data at the end of the process
@matianfu
matianfu / writable-stream-destroy.js
Last active May 6, 2020 08:48
Execution sequence of node stream.Writable during destroy
const hello = new require('stream').Writable({
destroy (err, callback) {
console.log('2 before callback in _destroy')
callback(err)
console.log('3 after callback in _destory')
}
}).on('error', err => console.log('6 error'))
.on('close', () => console.log('7 close'))
console.log('1 before destroy')
@matianfu
matianfu / writable-stream-end.js
Last active May 7, 2020 06:38
Execution sequence of node stream.Writable during end()
const chai = require('chai')
const expect = chai.expect
const hello = new require('stream').Writable({
autoDestroy: true,
destroy (err, callback) {
console.log('6 before callback in _destroy')
process.nextTick(() => console.log('i. before _destory callback nextTick'))
callback(err)