Skip to content

Instantly share code, notes, and snippets.

@navin-moorthy
Last active February 18, 2020 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navin-moorthy/6e1e7ac29398c0f600d9f80af1760c0e to your computer and use it in GitHub Desktop.
Save navin-moorthy/6e1e7ac29398c0f600d9f80af1760c0e to your computer and use it in GitHub Desktop.
Small Snippets
{
"scripts": [],
"showConsole": false
}
console.log("%c====================Start=======================", 'color: red');
// tonic ^6.0.0
const foo = (parameters, callback) => {
setTimeout(() => {
callback(parameters);
}, 100);
};
const curry = (method, ...args) => {
return (callback) => {
args.push(callback);
return method.apply({}, args);
};
};
const controller = (generator) => {
const iterator = generator();
const advancer = (response) => {
if (response && response.error) {
return iterator.throw(response.error);
}
const state = iterator.next(response);
if (!state.done) {
state.value(advancer);
}
}
advancer();
};
controller(function* () {
let a,
b,
c;
try {
a = yield curry(foo, 'a');
b = yield curry(foo, {error: 'Something went wrong.'});
c = yield curry(foo, 'c');
} catch (e) {
console.log(e);
}
console.log(a, b, c);
});
console.log('%c=====================End=======================', 'color: yellow');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment