2.0.1 • Published 6 years ago

promise-channel v2.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

promise-channel

Like Go Channels, but using JavaScript Promises.

Supports async-await and async-iteration.

npm install --save promise-channel

Example

This should look pretty familiar if you've used goroutines with go channels;

import Channel from "promise-channel";

var channel = new Channel();

channel.onClose.then(function(){
    console.log("Closed");
});

Promise.all([
    alice(channel),
    bob(channel),
]).then(function() {
    channel.close();
}).catch(function (e) {
    console.error(e.stack);
});

async function alice(channel) {
    var name = await channel.read();
    await channel.write("Hello, " + name);
}

async function bob(channel) {
    await channel.write("World");
    var greeting = await channel.read();

    console.log(greeting);
}

API

Channel

The module exports the Channel class which should be intstantiated with new

channel.read()

Attempts to read from the channel. Will throw if the channel is closed.

return

A promise which resolves to whatever value is written to the channel next.

channel.write(value)

Writes data to the channel. Will throw if the channel is closed.

arguments

  • value : The value to write to the channel

return

A promise which resolves once something has read from the channel.

channel.close(reason)

Closes the channel so that all pending and subsequent reads/writes will be rejected.

arguments

  • reason : An optional reason for closing the channel, will be passed to the onClose promise

channel.open

Boolean property which tells you whether the channel is currently open

channel.onClose

Read-only promise that resolves once the channel is closed

2.0.1

6 years ago

2.0.0

6 years ago

1.1.0

6 years ago

1.0.1

9 years ago

1.0.0

9 years ago