Skip to content

Instantly share code, notes, and snippets.

@montanaflynn
Created January 3, 2020 14:53
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 montanaflynn/f986fc4bafdf5de6197b6115de08f66b to your computer and use it in GitHub Desktop.
Save montanaflynn/f986fc4bafdf5de6197b6115de08f66b to your computer and use it in GitHub Desktop.
Example of providing error-first callback mechanism by default but also a chained .promise() method
// Example from https://nickmeldrum.com/blog/intriguing-reason-node-not-promises-by-default
const doS3GetObject = params => {
if (someErrorHappens) {
throw new Error('uh oh, we borked')
}
else {
return 'we done good' // imagine we actually did the work!
}
}
const s3 = {
getObject: (params, callback) => {
if (callback) {
try {
callback(null, doS3GetObject(params))
}
catch (e) {
callback(e)
}
}
return {
promise: () => new Promise((resolve, reject) => {
try {
resolve(doS3GetObject(params));
} catch (e) {
reject(e)
}
}),
}
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment