Skip to content

Commit

Permalink
[FABN-1000] Use less strict "is promise" check
Browse files Browse the repository at this point in the history
In complex Node.js applications, two or more promise implementations
may be available, providing different Promise classes. These different
Promise classes have the same standard interface, but you cannot rely
on instanceof to work as you would expect.

This has been seen as a problem in the ctor for AbstractEventStrategy
when running within VSCode, where an error is thrown because a promise
is not an instanceof Promise (even though it is a valid promise!).

Use the "is-promise" module instead which checks to see if the object
is "thenable" (has a .then()) method instead.

Change-Id: I6f47877b9be5815379163ffe5dc8d8cae1deba7b
Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone authored and harrisob committed Nov 6, 2018
1 parent 5fcf519 commit 0c12234
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion fabric-network/lib/impl/event/abstracteventstrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

'use strict';

const isPromise = require('is-promise');
const logger = require('fabric-network/lib/logger').getLogger('AbstractStrategy');

/**
Expand All @@ -24,7 +25,7 @@ class AbstractEventStrategy {
* @param {Promise.ChannelEventHub[]} eventHubsPromise Promise to event hubs for which to process events.
*/
constructor(eventHubsPromise) {
if (!(eventHubsPromise instanceof Promise)) {
if (!isPromise(eventHubsPromise)) {
const message = 'Expected event hubs to be a Promise but was ' + typeof eventHubsPromise;
logger.error('constructor:', message);
throw new Error(message);
Expand Down
1 change: 1 addition & 0 deletions fabric-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"types": "./types/index.d.ts",
"dependencies": {
"is-promise": "^2.1.0",
"nano": "^6.4.4",
"rimraf": "^2.6.2",
"uuid": "^3.2.1"
Expand Down

0 comments on commit 0c12234

Please sign in to comment.