sidorares / sidorares/node-mysql2

Fake then and catch functions on non-promise classes break duck typing

Open
#956 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4.4k
Forks
680
Avg merge
9h 7m
Merged PRs (30d)
59

Description

The non-promise version of Query defines fake then and catch functions that do nothing more than throwing an error with additional information about using the promise wrapper.
This works great for catching these kind of mistakes but it prevents the use of duck-typing to differentiate between promise- and non-promise api's.

The library I'm trying to use uses this check to test if an implementation is promise-based:

// '_' provided by the underscore module
if (promise && _.isFunction(promise.then) && _.isFunction(promise.catch)) {
	// Probably a promise.
	promise.then(function() {
		done(null);
	}).catch(function(error) {
		done(error);
	});
}

The non-promise implementation of Query passes this test because it defines these two functions. However, these methods do nothing more than throwing an error, which isn't particularly useful in this case.

I suggest removing these 'fake' methods to re-enable duck typing. I feel like the absence of then and catch when using them would be a clear enough sign to the user that the API they're using is not promise based. The process of using the API that is promise based is clearly documented, I don't think people will have trouble finding it. Additionally, I feel like it's just not the best practice to define a function whose only purpose is to notify the user that they shouldn't be using it, especially if that function is characteristic for a certain type of class.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the non-promise Query implementation in lib/commands/query.js, especially the linked then and catch definitions. Confirm how the non-promise and promise APIs are distinguished, then verify that the non-promise Query no longer passes the promise duck-typing check while its documented error behavior remains clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mysql, node.js
Domain
backend, database
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.