Skip to content

Instantly share code, notes, and snippets.

@nevyn
Created October 29, 2015 18:41
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 nevyn/2bb7998b289979f7b4df to your computer and use it in GitHub Desktop.
Save nevyn/2bb7998b289979f7b4df to your computer and use it in GitHub Desktop.
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()
// 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