Skip to content

Instantly share code, notes, and snippets.

@spion
Last active March 21, 2025 11:42
Show Gist options
  • Select an option

  • Save spion/8c9d8556697ed61108177164e90fb50d to your computer and use it in GitHub Desktop.

Select an option

Save spion/8c9d8556697ed61108177164e90fb50d to your computer and use it in GitHub Desktop.
function translateError(msg) {
var newErr = new Error(msg); // placed here to get correct stack
return e => {
newErr.originalError = e;
throw newErr;
}
}
async function asyncTask() {
const user = await UserModel.findById(1).catch(translateError('No user found'))
if(!user) throw new Error('No user found');
const savedTask = await TaskModel({userId: user.id, name: 'Demo Task'})
.catch(translateError('Error occurred while saving task'))
if(user.notificationsEnabled) {
await NotificationService.sendNotification(user.id, 'Task Created')
.catch(translateError('Error while sending notification'))
}
if(savedTask.assignedUser.id !== user.id) {
await NotificationService.sendNotification(savedTask.assignedUser.id, 'Task was created for you')
.catch(translateError('Error while sending notification'))
}
return savedTask
}
@phra

phra commented Apr 12, 2017

Copy link
Copy Markdown

i see some design flaws with this approach:

const p = new Promise((resolve, reject) => {
  return new Promise((r1, r2) => {
    throw new Error(42) // ERROR
  }).catch(e => reject(e))
})

async function main() {
  const res = await p.catch(e => { console.log('ERROR', e) })
  if (res) console.log('RES', res)
}

main()

and

const p = new Promise((resolve, reject) => {
  return new Promise((r1, r2) => {
    r2(42) // REJECTION
  }).catch(e => reject(e))
})

async function main() {
  const res = await p.catch(e => { console.log('ERROR', e) })
  if (res) console.log('RES', res)
}

main()

print out ERROR 42 and so they work correctly, BUT the following will not:

const p = new Promise((resolve, reject) => {
  return new Promise((r1, r2) => {
    r1(42) // RESOLVE
  }).catch(e => reject(e))
})

async function main() {
  const res = await p.catch(e => { console.log('ERROR', e) })
  if (res) console.log('RES', res)
}

main()

INSTEAD with these changes it will work:

const p = new Promise((resolve, reject) => {
  resolve(new Promise((r1, r2) => {
    r1(42)
  }).catch(e => reject(e)))
})

async function main() {
  const res = await p.catch(e => { console.log('ERROR', e) })
  if (res) console.log('RES', res)
}

main()

so as you can see in the case of nested promises, the catch error handling approach is not compatible for both success/failure scenarios.
what do you think about this, @spion?

@spion

spion commented Apr 19, 2017

Copy link
Copy Markdown
Author

I think you may've forgotten to paste the last two examples 😁 so I am not sure?

@CestDiego

Copy link
Copy Markdown

I think @phra has a point. @spion

@spion

spion commented Nov 14, 2017

Copy link
Copy Markdown
Author

Ok now I see it. No, that code has an error - returning a promise from a promise constructor doesn't do anything, that return value is ignored.

In the first examples it does something not because there is a return, but because an error is thrown and the catch statement rejects the outer promise.

In the non-working case an error is not thrown, so the catch statement doesn't run, so the outer promise never gets resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment