Back to articles
Decorating Promises Without Breaking Them
How-ToTools

Decorating Promises Without Breaking Them

via Dev.toGabor Koos

I wanted .get().json() . This came up while building convenience plugins for ffetch , a lightweight fetch wrapper focused on keeping native semantics intact. Libraries like ky solve the ergonomics problem by introducing a custom Response -like object, which works great until something outside the library expects a plain Response . I wanted a different path. Not because I needed it, strictly speaking. await fetch('/api/todos/1') followed by await response.json() works perfectly fine. But after the hundredth time writing that two-step dance across a codebase, you start reaching for something cleaner. The usual answer is a wrapper class or a custom Promise subclass. Both work, but both carry a hidden cost: you are now responsible for whatever happens when you swap out the native Response for your own abstraction. instanceof checks break. Framework integrations that inspect the response directly can behave unexpectedly. And the moment someone passes your custom object into something that e

Continue reading on Dev.to

Opens in a new tab

Read Full Article
1 views

Related Articles