DEV Community

Discussion on: An async/await gotcha

Collapse
 
tyrw profile image
Tyler Warnock

The use case was to create a wrapper for testing purposes, so that we could target parts of the code to wait for in our tests rather than using timeouts etc.

In production, the wrapper does nothing, but we overwrite that behavior in our tests so that we can call await asyncWrapper if needed. I may do an article elaborating on the use case, as it's been a really nice pattern for us.

I'm not sure where the "Duck" line gets drawn in JS, but we were using instanceof Promise because it felt the most direct. Checking for .then and typeof function is even duckier, though as I understand it that's the actual spec!

Collapse
 
cubiclebuddha profile image
Cubicle Buddha

Cool, yea it would be fun to see an article on that testing scenario.

Collapse
 
strahinjalak profile image
Strahinja Laktovic • Edited

One could always make new Promise and resolve inside the function that should be 'wrapped as async', and mock it as such. I don't event think setTimeout is necessary. I look forward to hearing test case elaboration as well. :)