Use is-promise to check if objects are Promises#321
Use is-promise to check if objects are Promises#321lilyissillyyy merged 2 commits intodiscordjs:masterfrom
Conversation
This adds support for alternative Promise implementations
|
Why do you need to use a package who have only 2 lines to check if it is a promise ? The problem with this package is that any object with a const isPromise = require('is-promise');
const object = {
then: () => null;
};
isPromise(object) // will return true
object instanceof Promise // will return false |
|
As for why you need a package, I guess you don't - if you'd rather copy that code locally and cite the source instead of adding a lightweight dependency, that's your call. Personally in my node development experience, the only times I've seen people averse to adding dependency packages for functionality are projects that are dependency-free to start with, which Commando clearly isn't. That said, whatever the maintainers prefer. As for any object with a |
|
@HarmoGlace (or anyone else who looks at these PRs) any more thoughts on this? It's been two weeks since I submitted this. Just to give a bit more justification, here is a minimal example of code that causes this problem:
// Use Bluebird promise implementation
global.Promise=require("bluebird");
const path = require('path');
const Commando = require('discord.js-commando');
const client = new Commando.Client();
client.registry.registerGroup('test', 'Test commands');
client.registry.registerCommandsIn(path.join(__dirname, 'commands'));
client.on('error', console.error);
client.login('<TOKEN>');
const Commando = require('discord.js-commando');
class TestCommand extends Commando.Command {
constructor(client) {
super(client, { name: 'test', group: 'test', memberName: 'test', description: 'Test the inhibitor'});
client.dispatcher.addInhibitor(message => {
return {reason: 'unauthorized', response: message.reply('this command can never be run')};
});
}
async run(message, args) {
return await message.reply("this should never happen");
}
}
module.exports = TestCommand;Error output when the |
This adds support for alternative Promise implementations.
In particular, we had seen several cases where our command Inhibitors were producing ugly error messages about invalid return types, even though we are returning what should be a valid value (an object with a string
reasonand a Promiseresponsegenerated by message.reply). For example:Switching to use is-promise resolves the issue by correctly handling all
thenables as Promises, including alternative Promise implementations like Bluebird.