WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@kay-is
Copy link
Contributor

@kay-is kay-is commented Sep 12, 2025

I got an error because instanceof wouldn't recognize res as a Promise.

Might be related to my usage of tsx, idk if it breaks the prototype chain or something.

Anyway, if I change to check for a then method, everything works again.

I got an error because `instanceof` wouldn't recognize `res` as a `Promise`. 

Might be related to my usage of `tsx`, idk.

Anyway, if I change to check for a `then` method, everything works again.
@usualoma
Copy link
Member

Oh, sorry, I accidentally approved it by mistake.

@usualoma
Copy link
Member

Hi @kay-is, thanks for creating the pull request.

I agree with the change to treat Thenable as a Promise.

I think the type error issue can be resolved like this.

diff --git i/src/listener.ts w/src/listener.ts
index 2724ffd..a9ea3dd 100644
--- i/src/listener.ts
+++ w/src/listener.ts
@@ -98,12 +98,15 @@ const responseViaCache = async (
   ;(outgoing as OutgoingHasOutgoingEnded)[outgoingEnded]?.()
 }
 
+const isPromise = (res: Response | Promise<Response>): res is Promise<Response> =>
+  typeof (res as Promise<Response>).then === 'function'
+
 const responseViaResponseObject = async (
   res: Response | Promise<Response>,
   outgoing: ServerResponse | Http2ServerResponse,
   options: { errorHandler?: CustomErrorHandler } = {}
 ) => {
-  if (res instanceof Promise) {
+  if (isPromise(res)) {
     if (options.errorHandler) {
       try {
         res = await res

Could you please add the tests? Then I think we can merge it.

@kay-is
Copy link
Contributor Author

kay-is commented Sep 13, 2025

@usualoma sure thing!

@yusukebe
Copy link
Member

Hi @kay-is, thank you for the PR.

I got an error because instanceof wouldn't recognize res as a Promise.

Can you reproduce it? If so, it's better to add a test to confirm that the issue will be resolved with this fix. If it's difficult, we can treat this change as a refactor with no tests.

@kay-is
Copy link
Contributor Author

kay-is commented Sep 15, 2025

@yusukebe sorry, no idea exactly how that error happened or how to reproduce it.

I just had a codebase where res wasn't an instance of Promise anymore. However, it was still a promise, just didn't have its prototype associated with the Promise class. Even your static file server did produce such kind of promise objects, so I'm quite confused.

I assume it has something to do with the tsx package or my tsconfig.json. Like, it affects all code, even the third-party code, of my codebase. But I'm not 100% sure.

@yusukebe yusukebe changed the title Refactor promise check for response handling fix: Refactor promise check for response handling Sep 16, 2025
Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

@kay-is

No problem! If so, let's go without tests. This is like a refactoring but resolving your issue. So, I'll release a new patch version after merging. Thank you for your contribution!

@yusukebe yusukebe merged commit 4f50527 into honojs:main Sep 16, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants