Description
Fibers are neat, but the majority of node.js developers don't and won't use them, and they don't work in browsers at all. It would be nice if we could provide a "classical FBP" system that works using standard JavaScript APIs.
Actually, after reading http://www.jpaulmorrison.com/fbp/noflo.html I'm pretty confused about how JSFBP is considered more "classical" than NoFlo. That page suggests the key difference is that NoFlow is single-threaded (because JavaScript is single-threaded) and thus not suitable for CPU-intensive programs, but then admits JSFBP is also single-threaded. So is the main difference actually the synchronous vs asynchronous style of programming?
Selective reading of ports and backpressure can both be accomplished in an asynchronous style. For example, Node-style Streams have pause
and resume
methods for handling backpressure. The port could buffer up to a fixed number of IPs then call pause
on the stream when the buffer fills up. The port could have a read
method that takes a callback (or returns a promise) which gets called when the next IP is available, and calls resume
on the stream if the buffer is no longer.
To take advantage of multiple cores you could use something like WebWorkers, though I think a generic system based on a standard IPC mechanism (e.x. ZeroMQ) and serialization protocol (e.x. Protocol Buffers) would be even cooler, allowing you to write components in any language, or even distribute processes across machines.
But anyway, there are several options for sync and async styles in JavaScript:
Asynchronous style:
- EventEmitters or Streams (see above)
- raw callbacks: probably Node-style
callback(err, result)
callbacks. This is my least favorite option, but is still the most popular style in Node.js. I probably wouldn't bother with this. - promises: promises are gaining a lot of traction in the JavaScript world lately, but they're still a very asynchronous style
Synchronous style:
- generators (ES6, can be transpiled to ES5): not available in browsers except via transpiling
- await (ES7, can be transpiled to ES5): lets you write synchronous style code that
await
s on promises - some other synchronous-style transpiler: e.x. streamline.js
Also this may be useful relevant reading: https://github.com/kriskowal/gtor
EDIT: task.js is another possibility: http://taskjs.org/