Created
October 29, 2015 18:41
-
-
Save nevyn/2bb7998b289979f7b4df to your computer and use it in GitHub Desktop.
This file contains hidden or 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
apa() | |
banan() | |
Coroutine(function() * { | |
foo() | |
bar = yield baz(); | |
bam = yield bar.work(); | |
values = yield _.map(omg, function(wtf) { | |
return wtf(); | |
}) | |
return bam | |
}) | |
kalle() | |
apa() | |
banan() | |
Future.task(function() { | |
foo() | |
bar = baz().wait() | |
bam = bar.work.wait() | |
values = _.each(omg, function(wtf) { | |
wtf().wait(); | |
}) | |
return bam | |
}) | |
kalle() | |
Fiber(function() { | |
a | |
b | |
c | |
}) | |
someAsyncFunction().then(function(firstAsyncValue) { | |
do stuff with async value | |
return someOtherAsyncFunction(firstAsyncValue) | |
}).then(function(nextValue) { | |
... | |
}).then(function(nextValue) { | |
}).catch(function(err) { | |
}) | |
syncCode() | |
Future.task(function() { | |
....wait() | |
... | |
.. | |
}) | |
syncCode() | |
// Wrong: Both synchronous and asynchronous errors | |
function Thing(a, b, c) { | |
if(!a) | |
throw "missing argument"; | |
return Promise(...) | |
} | |
try { | |
yay = Thing(...).catch(function() { | |
error handling | |
}) | |
} catch { | |
error handling | |
} | |
// Right: If a thing returns a promise, only ever deliver errors asynchronously and never throw errors. | |
function Thing(a, b, c) { | |
if(!a) | |
return Promise.failure(the error) | |
return Promise(...) | |
} | |
yay = Thing(...).catch(function() { | |
error handling | |
}) | |
// Note: You can still use throw! This code does not throw excetion: it fails the promise. | |
x = Coroutine(function() * { | |
throw "omg wtf" | |
}) | |
x.catch(function() { | |
// handle omg wtf | |
}) | |
// Same with Future | |
x = Future.task(function() { | |
throw "omg wtf" | |
}) | |
// throws exception HERE instead of the line above | |
x.wait() |
This file contains hidden or 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
// Wrong: Both synchronous and asynchronous errors | |
function Thing(a, b, c) { | |
if(!a) | |
throw "missing argument"; | |
return Promise(...) | |
} | |
try { | |
yay = Thing(...).catch(function() { | |
error handling | |
}) | |
} catch { | |
error handling | |
} | |
// Right: If a thing returns a promise, only ever deliver errors asynchronously and never throw errors. | |
function Thing(a, b, c) { | |
if(!a) | |
return Promise.failure(the error) | |
return Promise(...) | |
} | |
yay = Thing(...).catch(function() { | |
error handling | |
}) | |
// Note: You can still use throw! This code does not throw excetion: it fails the promise. | |
x = Coroutine(function() * { | |
throw "omg wtf" | |
}) | |
x.catch(function() { | |
// handle omg wtf | |
}) | |
// Same with Future | |
x = Future.task(function() { | |
throw "omg wtf" | |
}) | |
// throws exception HERE instead of the line above | |
x.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment