release-zalgo

WebJar for release-zalgo

License

License

ISC
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

release-zalgo
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

release-zalgo
WebJar for release-zalgo
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/novemberborn/release-zalgo

Download release-zalgo

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/release-zalgo/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>release-zalgo</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/release-zalgo/
implementation 'org.webjars.npm:release-zalgo:1.0.0'
// https://jarcasting.com/artifacts/org.webjars.npm/release-zalgo/
implementation ("org.webjars.npm:release-zalgo:1.0.0")
'org.webjars.npm:release-zalgo:jar:1.0.0'
<dependency org="org.webjars.npm" name="release-zalgo" rev="1.0.0">
  <artifact name="release-zalgo" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='release-zalgo', version='1.0.0')
)
libraryDependencies += "org.webjars.npm" % "release-zalgo" % "1.0.0"
[org.webjars.npm/release-zalgo "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : es6-error jar [4.0.1,5)

Project Modules

There are no modules declared in this project.

release-zalgo

Helps you write code with promise-like chains that can run both synchronously and asynchronously.

Installation

$ npm install --save release-zalgo

Usage

If you use this module, you'll release Ẕ̶̨̫̹̌͊͌͑͊̕͢͟a̡̜̦̝͓͇͗̉̆̂͋̏͗̍ͅl̡̛̝͍̅͆̎̊̇̕͜͢ģ̧̧͍͓̜̲͖̹̂͋̆̃̑͗̋͌̊̏ͅǫ̷̧͓̣͚̞̣̋̂̑̊̂̀̿̀̚͟͠ͅ. You mustn't do that.

Before you proceed, please read this great post by Isaac Schlueter on Designing APIs for Asynchrony.

The first rule of using this package is to keep your external API consistent.

The second rule is to accept the burden of controlling Ẕ̶̨̫̹̌͊͌͑͊̕͢͟a̡̜̦̝͓͇͗̉̆̂͋̏͗̍ͅl̡̛̝͍̅͆̎̊̇̕͜͢ģ̧̧͍͓̜̲͖̹̂͋̆̃̑͗̋͌̊̏ͅǫ̷̧͓̣͚̞̣̋̂̑̊̂̀̿̀̚͟͠ͅ by ensuring he does not escape your API boundary.

With that out of the way… this package lets you write code that can run both synchronously and asynchronously. This is useful if you have fairly complex logic for which you don't want to write multiple implementations. See package-hash for instance.

This is best shown by example. Let's say you have a hashFile() function:

const crypto = require('crypto')
const fs = require('fs')

function hashFile (file) {
  return new Promise((resolve, reject) => {
    fs.readFile(file, (err, buffer) => err ? reject(err) : resolve(buffer))
  })
    .then(buffer => {
      const hash = crypto.createHash('sha1')
      hash.update(buffer)
      return hash.digest('hex')
    })
}

A synchronous version could be implemented like this:

function hashFileSync (file) {
  const buffer = fs.readFileSync(file)
  const hash = crypto.createHash('sha1')
  hash.update(buffer)
  return hash.digest('hex')
}

Here's the version that uses release-zalgo:

const crypto = require('crypto')
const fs = require('fs')

const releaseZalgo = require('release-zalgo')

const readFile = {
  async (file) {
    return new Promise((resolve, reject) => {
      fs.readFile(file, (err, buffer) => err ? reject(err) : resolve(buffer))
    })
  },

  sync (file) {
    return fs.readFileSync(file)
  }
}

function run (zalgo, file) {
  return zalgo.run(readFile, file)
    .then(buffer => {
      const hash = crypto.createHash('sha1')
      hash.update(buffer)
      return hash.digest('hex')
    })
}

function hashFile (file) {
  return run(releaseZalgo.async(), file)
}

function hashFileSync (file) {
  const result = run(releaseZalgo.sync(), file)
  return releaseZalgo.unwrapSync(result)
}

Note how close the run() implementation is to the original hashFile().

Just don't do this:

function badExample (zalgo, file) {
  let buffer
  zalgo.run(readFile, file)
    .then(result => { buffer = result })

  const hash = crypto.createHash('sha1')
  hash.update(buffer)
  return hash.digest('hex')
}

This won't work asynchronously. Just pretend you're working with promises and you'll be OK.

API

First require the package:

const releaseZalgo = require('release-zalgo')

releaseZalgo.sync()

Returns a zalgo object that runs code synchronously:

const zalgo = releaseZalgo.sync()

releaseZalgo.async()

Returns a zalgo object that runs code asynchronously:

const zalgo = releaseZalgo.async()

releaseZalgo.unwrapSync(thenable)

Synchronously unwraps a thenable, which is returned when running synchronously. Returns the thenables fulfilment value, or throws its rejection reason. Throws if the thenable is asynchronous.

zalgo.run(executors, ...args)

When running synchronously, executors.sync() is called. When running asynchronously executors.async() is used. The executer is invoked immediately and passed the remaining arguments.

For asynchronous execution a Promise is returned. It is fulfilled with executors.async()'s return value, or rejected if executors.async() throws.

For synchronous execution a thenable is returned. It has the same methods as Promise except that callbacks are invoked immediately. The thenable is fulfilled with executors.sync()'s return value, or rejected if executors.sync() throws.

zalgo.all(arr)

When running synchronously, returns a new thenable which is fulfilled with an array, after unwrapping all items in arr.

When running asynchronously, delegates to Promise.all(arr).

zalgo.returns(value)

When running synchronously, returns a new thenable which is fulfilled with value.

When running asynchronously, delegates to Promise.resolve(value).

zalgo.throws(reason)

When running synchronously, returns a new thenable which is rejected with reason.

When running asynchronously, delegates to Promise.reject(reason).

Thenables

Thenables are returned when running sychronously. They're much like Promises, in that they have then() and catch() methods. You can pass callbacks and they'll be invoked with the fulfilment value or rejection reason. Callbacks can return other thenables or throw exceptions.

Note that then() and catch() must be called on the thenable, e.g. thenable.then(), not (thenable.then)().

Thenables should not be exposed outside of your API. Use releaseZalgo.unwrapSync() to unwrap them.

Versions

Version
1.0.0