r/javascript • • Apr 23 '19

Introducing Node.js 12

https://medium.com/@nodejs/introducing-node-js-12-76c41a1b3f3f
333 Upvotes

85 comments sorted by

View all comments

Show parent comments

1

u/ilikepugs Apr 24 '19

And if you write a lot of interface-based code it's not uncommon.

1

u/blood_bender Apr 24 '19

Curious, how come?

4

u/ilikepugs Apr 24 '19

Let's say you design an interface for some data fetching util, but you ship different implementations to different clients/platforms, and the appropriate implementation is DI'd at runtime.

Well consider the method that actually performs the request. It'll be async. But in some cases (namely tests in this example) you might want to return some stubbed data for your test instead. So in your test implementation of the util that "async" method just returns your dummy JSON and you don't need to wrap it in a promise.

This is a fairly contrived example but yeah.

2

u/blood_bender Apr 24 '19

Oh that's fair. There have been times where I have an async function return a cached response instead of fetching a new one, so that's another use case that I've written. But really I'd guess 99% of async functions are actually awaiting or returning a Promise directly.

1

u/ilikepugs Apr 24 '19

Lol your example is much better, cheers. 😁