1.4.2 • Published 6 years ago

yapc v1.4.2

Weekly downloads
62
License
-
Repository
github
Last release
6 years ago

Yet Another Promise Chainer

  • Native promise-based method chaining
  • ES6 Class-based, easily extensible
  • Built for async/await
  • Zero dependencies

Usage

const fs = require('fs')
const { Chainable } = require('./index')

class MyChainableClass extends Chainable {
  // Chain any method that returns a promise
  loadFile(filePath) {
    return new Promise((resolve, reject) => {
      fs.readFile(filePath, (err, file) => {
        if (err) throw err

        this.file = file
        resolve(this.file)
      })
    })
  }

  // Execute non-promise functions in the order chained
  transformFile(handler) {
    if (!this.file) throw new Error('File not loaded!')
    this.fileTransformed = handler(this.file)
    return this.fileTransformed
  }

  // Returns output at the end of the chain
  outputResults() {
    const output = (this.fileTransformed || this.file).toString()
    process.stdout.write(output)
    return output
  }
}

;(async () => {
    const chain = new MyChainableClass()

    // Build the chain of operations
    chain
      .loadFile('./package.json')
      .transformFile((file) => new Buffer.from(
        file.toString().replace('yapc', 'sucky-package')
      ))
      .outputResults()

    // Execute, sequentially, in the order called
    console.log('modified package.json:', await chain)
})()
1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.7

6 years ago

1.2.6

6 years ago

1.2.5

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.0.1

6 years ago