Skip to content

Instantly share code, notes, and snippets.

@hoodwink73
Last active April 15, 2019 06:14
Show Gist options
  • Save hoodwink73/b5b8059028a899d811802fb4f45ba7ef to your computer and use it in GitHub Desktop.
Save hoodwink73/b5b8059028a899d811802fb4f45ba7ef to your computer and use it in GitHub Desktop.
Error handling with Asynquence is a bit different. By default a error in the sequence suspends the entire sequence.
ASQ()
.promise(delayWithReject(200))
.or(() => console.log('caught'))
.val(() => console.log('The sequence deactivates. So this never gets logged.'))
ASQ()
// a `try` always ends up in a sequence progress
// in case the task fails the next step will receive a special messsage
// -> {error: "Reason for failure" }
.try((done) => done.fail('Doomed!!'))
.or(() => console.log('caught'))
.val((message) => console.log('The sequence would proceed either way', message))
var ASQ = require('asynquence-contrib')
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const delayWithReject = (ms) => new Promise((resolve, reject) => setTimeout(reject, ms))
delayWithReject(200)
.catch(() => console.error('caught'))
.then(() => delay(200))
.then(() => console.log('The chain is restored'))
// unhandled error does not restore the chain
delayWithReject(400)
.then(() => delay(200))
.then(() => console.log('The chain never gets restored'))
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const delayWithReject = (ms) => new Promise((resolve, reject) => setTimeout(reject, ms))
ASQ()
.promise(delay(200))
.val(() => {console.log('boom')})
.or(() => {console.error('bam')}) // this gets logged
.promise(delayWithReject(400))
// my expected behavior is to see just this get logged
.or(() => console.error('caught')) // this also gets logged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment