Skip to content

Instantly share code, notes, and snippets.

@tunnckoCore
Last active March 1, 2017 11:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tunnckoCore/fd4d86b5dd62e5b1c10d to your computer and use it in GitHub Desktop.
Save tunnckoCore/fd4d86b5dd62e5b1c10d to your computer and use it in GitHub Desktop.
Async waterfall in 131bytes (short) / 209bytes (long) with custom scopes
// 131bytes/short version
// jsbin demo http://jsbin.com/hakila/5/edit?console,js
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}()}
// 209bytes/long version
// jsbin demo http://jsbin.com/hakila/5/edit?console,js
function awf(scope, funcs, end) {
(function next() {
funcs.length > 0 ? funcs.shift().apply(scope || {}, [].slice.call(arguments).concat(next)) : 0;
funcs.length == 0 ? end.call(scope) : 0;
})();
}
// jsbin demo http://jsbin.com/hakila/5/edit?console,js
var obj = {
stage1: 0,
stage2: 10,
nested: {
observer: 200,
finish: 'end'
}
};
//scope: obj
awf(obj, [
function step1(next) {
console.log('obj.stage1 at `step1`:'+this.stage1);
next();
},
function step2(next) {
console.log('obj.nested.observer at `step2`:'+this.nested.observer);
console.log('increment it with 100');
var newObserverValue = this.nested.observer+100
next(newObserverValue);
},
function step3(newObserver, next) {
console.log('obj.nested.observer at `step3` is '+newObserver+' - incremented at `step2`, now setTimeout at `step3`');
setTimeout(function() {
newObserver = newObserver+200
console.log('obj.nested.observer at `step3`: '+newObserver);
next(newObserver);
}, 500);
},
function step4(newNewObserverValue, next) {
//next is undefined, not call it
console.log('obj.nested.observer now at `step4: '+newNewObserverValue)
},
],
function done() {
console.log('example 1 finished');
console.log('------- wait 3s for next example to load ------');
});
//scope:obj.nested
//will set timeout 3000ms for demo reasons
setTimeout(function() {
awf(obj.nested, [
function one(next) {
console.log('obj.nested.observer inital: '+this.observer);
var self = this, obsrv;
setTimeout(function() {
obsrv = self.observer+100;
console.log('obj.nested.observer increments at `one` with 100 and proceed');
next(obsrv);
}, 200);
},
function two(obsrv, next) {
var self = this;
setTimeout(function() {
console.log('obj.nested.observer at `two`: '+obsrv);
next(obsrv);
}, 500);
},
function tree(obsrvFinal, next) {
// next == undefined, cuz no more funcs in waterfall
console.log('obj.nested.observer at `tree`: '+(obsrvFinal+1));
}],
function done() {
console.log(this.finish + '`ed')
}
);
},3000);
function catchy(funcs, end) {
var reject = function reject(err) {
funcs = [];
end.call(this, err);
return;
};
var resolve = function resolve() {
if (funcs.length === 0) {
end.call(this, null, [].slice.call(arguments))
return;
}
if (funcs && Array.isArray(funcs)) {
funcs.shift().apply(this, [].slice.call(arguments).concat([resolve, reject]))
}
};
setTimeout(resolve, 0)
}
var data = 'str'
catchy([
function then1(resolve, reject) {
if (typeof data == 'string') {
console.log('1 [pass] $data is `string`')
resolve(data+123)
return;
}
reject(new TypeError('1 [fail] $data must be `string`'))
},
function then2(data, resolve, reject) {
if (data == 'str123') {
console.log('2 [pass] $data is `str123`')
setTimeout(function() {
resolve([data, 123, true])
}, 500)
return;
}
reject(new Error('2 [fail] $data must be `str123`'))
},
function then3(data, resolve, reject) {
if (Array.isArray(data) && data.length == 3) {
console.log('3 [pass] $data is `array`')
console.log('3 [pass] $data is with length 3')
resolve(data);
return;
}
reject(new Error('3 [fail] $data must be array with length 3'))
},
function then4(data, resolve, reject) {
if (data[1] === 123) {
setTimeout(function() {
console.log('4 [pass] $data[1] is `123`, convert to `yes123`')
data[1] = 'yes123'
resolve(data, 'hello here')
},200)
return;
}
reject(new Error('4 [fail] $data[1] should be `123` {Number}'))
},
function then5(data, second, resolve, reject) {
if (data[1] === 'yes123' && second === 'hello here') {
console.log('5 [pass] $data[1] is `yes123`')
console.log('5 [pass] $second is `hello here`')
resolve(12, data, second)
return;
}
reject(new Error('5 [fail] $data[1] should be `yes123` {String} and $second should be `hello here`'))
}],
function done(err, res) {
if (err) {
console.error(err)
return;
}
console.log('Completed successfully!', res)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment