Skip to content

Instantly share code, notes, and snippets.

@paulodiovani
Last active April 25, 2018 16:26
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 paulodiovani/1cb41a752b058afd8037613ef33da704 to your computer and use it in GitHub Desktop.
Save paulodiovani/1cb41a752b058afd8037613ef33da704 to your computer and use it in GitHub Desktop.
Example of progressive rollback
/*
* Example of handling promises with progressive rollback
* Usage: node using_async_await.js 5
*/
// await doesn't work in global scope, must wrap
(async function() {
const errored = process.argv[2] //get from command line argument
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// the functions who triggers and returns the promises
const createPromise = async (index) => {
await timeout(1000)
if (errored == index) {
const error = new Error(`promise ${index} rejected`)
error.index = index
throw error
}
return { index }
}
// the function which triggets the rollback for previous steps
const createRollbackPromise = async (index) => {
console.log(`rollbackpromise ${index}`)
}
// default success
const onSuccess = async ({ index }) => {
console.log(`promise ${index} resolved`)
}
// error threatment and rollback procedure
const onError = async (error) => {
console.error(error.message)
const rollbackArray = []
const index = error.index
if (!index) return console.error(error)
for (let j = index -1; j >= 1; j--) {
await createRollbackPromise(j) //can't run parallel with awa
}
}
try {
await onSuccess(await createPromise(1))
await onSuccess(await createPromise(2))
await onSuccess(await createPromise(3))
await onSuccess(await createPromise(4))
await onSuccess(await createPromise(5))
} catch(error) {
await onError(error)
}
})()
/*
* Example of handling promises with progressive rollback
* Usage: node using_promises.js 5
*/
const errored = process.argv[2] //get from command line argument
// the functions who triggers and returns the promises
const createPromise = (index) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (errored == index) {
const error = new Error(`promise ${index} rejected`)
error.index = index
return reject(error)
}
resolve({ index })
}, 1000)
})
}
// the function which triggets the rollback for previous steps
const createRollbackPromise = (index) => {
return Promise.resolve(`rollbackpromise ${index}`)
.then(console.log)
}
// default success
const onSuccess = ({ index }) => {
console.log(`promise ${index} resolved`)
}
// error threatment and rollback procedure
const onError = (error) => {
console.error(error.message)
const rollbackArray = []
const index = error.index
if (!index) return console.error(error)
for (let j = index -1; j >= 1; j--) {
rollbackArray.push(createRollbackPromise(j))
}
// I suppose rollbacks can run in parallel
return Promise.all(rollbackArray)
}
// processing promises
createPromise(1)
.then(onSuccess)
.then(() => createPromise(2))
.then(onSuccess)
.then(() => createPromise(3))
.then(onSuccess)
.then(() => createPromise(4))
.then(onSuccess)
.then(() => createPromise(5))
.then(onSuccess)
.catch(onError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment