Skip to content

Instantly share code, notes, and snippets.

@tunnckoCore
Last active June 6, 2018 05:10
Show Gist options
  • Save tunnckoCore/4e8fddee5ceb4fe26613ab25be0ac953 to your computer and use it in GitHub Desktop.
Save tunnckoCore/4e8fddee5ceb4fe26613ab25be0ac953 to your computer and use it in GitHub Desktop.
Ultimate and asynchronous control flow plugin system.

And btw, arguments passed to each plugin and their context are configurable through options.
All of this works and is reality.

app
  .use(function one (done) {
    this.one = 'one'
    // console.log('first:', this.one)
    // throw new Error('foo')
    fs.readFile('package.json', done)
  })
  .use(function two () {
    this.two = 'two'
    // console.log('second:', this.two)
    // console.log('args:', a, b, c) // => 1 2 3
    // throw new Error('foo')
    return fs.readFileSync('not exist', 'utf8')
  })
  .use(function * gen () {
    return Promise.resolve(123)
  })
  .use(function syncness () {
    return 777
  })
  .use(function sync () {
    return function (done) {
      done(null, [555, 23])
    }
  })
  .use(function syncReturnPromise () {
    return function promise () {
      return Promise.resolve(555)
    }
  })
  .use(function three (done) {
    this.three = 'three'
    // console.log('third:', this.three)
    // console.log('ctx:', this)
    fs.stat('./test.js', done)
  })
  .use(function nested () {
    // console.log('this1:', this)
    return function nested2 () {
      this.foo = 1488
      // console.log('this2:', this)
      return function sync () {
        // console.log('this3:', this)
        return Promise.resolve([8,7,6])
      }
    }
  })
  .use(function nesting () {
    return function foobar () {
      return function extraLoud () {
        // console.log('this.foo === 1488?', this.foo === 1488)
        return function beta (done) {
          dsfsdf // error
          done(null, 123)
        }
      }
    }
  })
  .use(function sasasa () {
    return function foobar () {
      return function nestedCb () {
        return function sync () {
          return 123
        }
      }
    }
  })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment