1.1.0 • Published 5 years ago

yield-msg v1.1.0

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

yield-msg

A utility to create inter-function communication systems using generators.

Installation

$ npm install yield-msg

Usage

const yieldmsg = require("yield-msg");

const fn = yieldmsg(function(x) {
    return x * 2; // This is a replier function
});

var result = fn(function*() {
    var x = 5;
    var y = yield x;
    return y;
});

console.log(result); // 10

yieldmsg takes a second argument: processed.

If the processed argument is truthy (the default), then yielding arrays will pass each value of the array through the replier.

If it is falsy, the whole array will be passed through the replier. Here is an example demonstrating that:

const fn = yieldmsg(function(x) {
    return x * 2;
});

var result = fn(function*() {
    var xs = [1, 2, 3];
    var ys = yield xs;
    return ys;
});

console.log(result); // [2, 4, 6]

const processingDisabledFn = yieldmsg(function(xs) {
    return xs.concat([4, 5]);
}, false);

var res = processingDisabledFn(function*() {
    var xs = [1, 2, 3];
    var ys = yield xs;
    return ys;
});

console.log(res); // [1, 2, 3, 4, 5]

Async version

yieldmsg.async is an async version of yieldmsg. It accepts an asyncronous replier and returns a promise that resolves once the replier does for all of the yields.

License

yield-msg is licensed under the MIT License.