Last active
February 18, 2020 09:40
-
-
Save navin-moorthy/6e1e7ac29398c0f600d9f80af1760c0e to your computer and use it in GitHub Desktop.
Small Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scripts": [], | |
"showConsole": false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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