async3
v0.0.2
Published
Procedural asynchronous flow control for JavaScript in Node.JS and browsers.
Downloads
2
Readme
async3.js
Procedural asynchronous flow control for JavaScript in Node.JS and browsers.
For people who find continuation passing style (CPS) more reasonable than Promises, Futures, generators, etc.
Inspired by async2, mini-async.
Motivation
Javascript, like many languages
that support passing callbacks to asynchronous APIs,
provides flow control keywords (i.e, if
, while
, for
)
that are nearly useless once you need to await the future
result.
At some point you realize you need to take a procedural approach and reimplement these keywords as functions.
In doing so, if you are familiar with assembly or machine code,
you may realize that all flow control operations can actually
be reduced to conditional JMP opcodes. Trendy variations like
unless
, until
, do...while
, etc. are unecessary
syntactic sugar.
Taking the minimalist approach, you realize you can get a large degree of control with just a few functions that are a few lines of code each. No need for object instantiations, or multiple function calls in a builder pattern.
Include
ES6 Node.JS:
const {ifAsync} = require('async3');
Flow Control Functions
- ifAsync : conditional branching without two callback references
- whileAsyncSerial: latched infinite loop
- forEachAsyncSerial : serial one-at-a-time invocation
- forEachAsyncParallel : parallel branch-join invocation
- forEachAsyncParallelBatch : parallel with limited maximum concurrent invocations
Async3.ifAsync()
For when you have two functions: one that is asynchronous but is only executed sometimes, and one that is called afterward. Without this, you'd have to declare or reference the second function twice.
Syntax
void ifAsync(test, true_cb[, ...args], done_cb);
Parameters
test
Truthy value determines whether to invoketrue_cb
.true_cb
The function invoked if thetest
function above returns truthy. Accepts no targ Expected to be asynchronous. Accepts arguments provided in...args
parameter described below. Void return value....args
Optional. One or more arguments of any type, to be passed intotrue_cb
function.done_cb
Function invoked aftertrue_cb
completes. Invoked regardless of whethertest
returned truthy.
Example
ifAsync(tmpAbsPath != absPath,
exec, 'mv "'+ tmpAbsPath +'" "'+ absPath +'"', () =>
{
console.log('placed locally for later static upload to cdn as '+ relPath);
});