4 ms·
> Every async function needs a new Promise, and needs to resolve(). It won’t complain if you don’t do this, but it also won’t actually wait. The debugging aroun
by WorldMaker 7y ago
> Every async function needs a new Promise, and needs to resolve(). It won’t complain if you don’t do this, but it also won’t actually wait. The debugging around this is super annoying.
Async functions manage the "outer" promise automatically for you. It looks like you are "double-wrapping" promises unnecessarily by creating manual promises around a library that already looks to be returning Promises (or at least, is thenable and Promise-like).
So far as I can tell, it could be simplified to:
async function getEmailOfCourseWithCourseId(courseId) { // async important
try {
const course = await doAsyncStuffWithFirestore(courseId)
return course.email
} catch (error) {
console.error(error)
}
}
async function sendEmailInSendgrid(fields, courseEmail) { // async important
try {
const msg = {to: courseEmail, from: fields.from, text: fields.text}
await doAsyncStuffWithSendGrid(fields, courseEmail)
return msg
} catch (error) {
console.error(error)
}
}
async function saveToCloudFirestore(fields, courseEmail, courseId) { // async important
try {
return await doAsyncStuffWithFirestore(fields, courseEmail, courseId)
} catch (error) {
console.error(error))
}
}
The thing that stands out refactoring it to use awaits is that your try {} catch {} may be too low and you should move the try {} catch {} up higher in your call stack. (Do you really want to ignore the error and continue with all of these inner async functions? Because that is what you are currently doing.)
- nikodunk 7y agoThis is an excellent comment. Thank you! I see what you mean – I am double-wrapping with Promises and could replace this with try{} catch{}. My error was that I previously simply wrote return course.email or whatever, which as far as I could tell did not make the outer function wait. I will correct this in the article once I've tested it.
- WorldMaker 7y agoYeah, the trick to remember is the await in the line before (and in general, anywhere you would .then() you can await, .catch() you can try/catch). You'll notice you can even `return await`, and that may have been the particular missing part you wanted. Where promises typically auto "flatten" their returns (return a Promise<T> in .then() and the return of .then() is a Promise<T> not a Promise<Promise<T>>), async/await does not flatten the promise automatically in case you meant to do that. If you are writing a lot of async/await code, you may want to take the time to configure your linter for promises/async/await or try Typescript, as either or both can be very useful tools at spotting cases where you missed an await before a Promise.
- johnsonjo 7y agoAlso a thing to note if you want just like with normal try catches you could catch certain errors that you know how to handle in that higher level of the call stack and you can rethrow errors that you don't know how to handle using `throw <error-variable-name-here>` to be handled in a lower part of your call stack.