Skip to content

Commit

Permalink
Add isPromise(). Closes #346
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Oct 31, 2019
1 parent b9aa286 commit 83019b8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
8 changes: 8 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,11 @@ await Hoek.wait(2000); // waits for 2 seconds

#### block()
A no-op Promise. Does nothing.


#### isPromise(promise)

Determines if an item is a promise where:
- `promise` - the item being tested.

Returns `true` is the item is a promise, otherwise `false`.
10 changes: 10 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,16 @@ export function wait(timeout?: number): Promise<void>;
export function block(): Promise<void>;


/**
* Determines if an object is a promise.
*
* @param promise - the object tested.
*
* @returns true if the object is a promise, otherwise false.
*/
export function isPromise(promise: any): boolean;


export namespace ts {

/**
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
flatten: require('./flatten'),
ignore: require('./ignore'),
intersect: require('./intersect'),
isPromise: require('./isPromise'),
merge: require('./merge'),
once: require('./once'),
reach: require('./reach'),
Expand Down
9 changes: 9 additions & 0 deletions lib/isPromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const internals = {};


module.exports = function (promise) {

return !!promise && typeof promise.then === 'function';
};
29 changes: 29 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2767,3 +2767,32 @@ describe('stringify()', () => {
expect(Hoek.stringify(obj)).to.contain('Cannot display object');
});
});

describe('isPromise()', () => {

it('determines if an object is a promise', async () => {

expect(Hoek.isPromise({})).to.be.false();
expect(Hoek.isPromise(null)).to.be.false();
expect(Hoek.isPromise(false)).to.be.false();
expect(Hoek.isPromise(0)).to.be.false();
expect(Hoek.isPromise('')).to.be.false();
expect(Hoek.isPromise({ then: 1 })).to.be.false();
expect(Hoek.isPromise([])).to.be.false();

const items = [
Promise.resolve(),
Promise.reject()
];

expect(Hoek.isPromise(items[0])).to.be.true();
expect(Hoek.isPromise(items[1])).to.be.true();
expect(Hoek.isPromise(new Promise(Hoek.ignore))).to.be.true();
expect(Hoek.isPromise({ then: Hoek.ignore })).to.be.true();

try {
await Promise.all(items);
}
catch (err) { }
});
});
12 changes: 12 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ expect.error(Hoek.block(123));
// $lab:types:on$


// isPromise()

Hoek.isPromise(1);
Hoek.isPromise({});
Hoek.isPromise(null);

expect.type<boolean>(Hoek.isPromise(1));

expect.error(Hoek.isPromise());
expect.error(Hoek.isPromise(1, 2));


// ts

interface X { a: number; };
Expand Down

0 comments on commit 83019b8

Please sign in to comment.