Skip to content

Instantly share code, notes, and snippets.

@tunnckoCore
Forked from 140bytes/LICENSE.txt
Last active March 8, 2018 11:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tunnckoCore/f4697abcd6ebbdd4ccce to your computer and use it in GitHub Desktop.
Save tunnckoCore/f4697abcd6ebbdd4ccce to your computer and use it in GitHub Desktop.
async-waterfall in 131 bytes! Runs an array of functions in series, each passing their results to the next in the array and all of them can have initial context.

async waterfall

Runs an array of functions in series, each passing their results to the next in the array and all of them can have initial context. However, this piece of code not handle errors. So if any of functions throw error it is thrown directly. lol.

If you want something more robust so you should try

http://npm.im/catchy - very similar, but plays like promises - with context, resolve and reject.

Usage example

function awf(n,t,l){!function c(){t.length>0?t.shift().apply(n||{},[].slice.call(arguments).concat(c)):0,0==t.length?l.call(n):0}()}

awf({
  foo: 'bar',
  baz: 1
}, [
  function step1(next) {
    console.log('step1');
    next();
  },
  function step2(next) {
    console.log('step2');
    console.log('increment baz with 100');
    this.baz += 100;

    next(this.baz);
  },
  function step3(baz, next) {
    var self = this;
    console.log('step3');
    console.log('baz is ' + baz);

    self.foo = 'foo_step3'
    console.log('foo changed to ' + self.foo);

    setTimeout(function() {
      console.log('step3:setTimeout');
      console.log('increment baz with 200');

      self.baz += 200;

      next(self.baz);
    }, 500);
  },
  function step4(baz301, next) {
    console.log('step4');
    console.log('context object is ' + JSON.stringify(this, null, 2));
    console.log('baz is 301: ' + baz301)
  },
], function done() {
  console.log('done');
});
function awf(
scope,
funcs,
done
) {
(function next() {
funcs.length > 0
? funcs.shift().apply(scope || {}, [].slice.call(arguments).concat(next))
: 0;
funcs.length === 0
? done.call(scope)
: 0;
})();
}
function(n,t,l){!function c(){t.length>0?t.shift().apply(n||{},[].slice.call(arguments).concat(c)):0,0===t.length?l.call(n):0}()}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011-2015 Charlike Mike Reagent <http://j.mp/1stW47C>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "asyncWaterfall",
"description": "Runs an array of functions in series, each passing their results to the next in the array and all of them can have initial context.",
"keywords": [
"async",
"waterfall",
"array",
"series",
"control",
"flow"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>undefined</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(){ /* the code here should be identical to the entry. */ }
document.getElementById( "ret" ).innerHTML = myFunction()
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment