Skip to content

Commit 83019b8

Browse files
committed
Add isPromise(). Closes #346
1 parent b9aa286 commit 83019b8

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

API.md

+8
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,11 @@ await Hoek.wait(2000); // waits for 2 seconds
393393

394394
#### block()
395395
A no-op Promise. Does nothing.
396+
397+
398+
#### isPromise(promise)
399+
400+
Determines if an item is a promise where:
401+
- `promise` - the item being tested.
402+
403+
Returns `true` is the item is a promise, otherwise `false`.

lib/index.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,16 @@ export function wait(timeout?: number): Promise<void>;
445445
export function block(): Promise<void>;
446446

447447

448+
/**
449+
* Determines if an object is a promise.
450+
*
451+
* @param promise - the object tested.
452+
*
453+
* @returns true if the object is a promise, otherwise false.
454+
*/
455+
export function isPromise(promise: any): boolean;
456+
457+
448458
export namespace ts {
449459

450460
/**

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
flatten: require('./flatten'),
2020
ignore: require('./ignore'),
2121
intersect: require('./intersect'),
22+
isPromise: require('./isPromise'),
2223
merge: require('./merge'),
2324
once: require('./once'),
2425
reach: require('./reach'),

lib/isPromise.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const internals = {};
4+
5+
6+
module.exports = function (promise) {
7+
8+
return !!promise && typeof promise.then === 'function';
9+
};

test/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -2767,3 +2767,32 @@ describe('stringify()', () => {
27672767
expect(Hoek.stringify(obj)).to.contain('Cannot display object');
27682768
});
27692769
});
2770+
2771+
describe('isPromise()', () => {
2772+
2773+
it('determines if an object is a promise', async () => {
2774+
2775+
expect(Hoek.isPromise({})).to.be.false();
2776+
expect(Hoek.isPromise(null)).to.be.false();
2777+
expect(Hoek.isPromise(false)).to.be.false();
2778+
expect(Hoek.isPromise(0)).to.be.false();
2779+
expect(Hoek.isPromise('')).to.be.false();
2780+
expect(Hoek.isPromise({ then: 1 })).to.be.false();
2781+
expect(Hoek.isPromise([])).to.be.false();
2782+
2783+
const items = [
2784+
Promise.resolve(),
2785+
Promise.reject()
2786+
];
2787+
2788+
expect(Hoek.isPromise(items[0])).to.be.true();
2789+
expect(Hoek.isPromise(items[1])).to.be.true();
2790+
expect(Hoek.isPromise(new Promise(Hoek.ignore))).to.be.true();
2791+
expect(Hoek.isPromise({ then: Hoek.ignore })).to.be.true();
2792+
2793+
try {
2794+
await Promise.all(items);
2795+
}
2796+
catch (err) { }
2797+
});
2798+
});

test/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,18 @@ expect.error(Hoek.block(123));
322322
// $lab:types:on$
323323

324324

325+
// isPromise()
326+
327+
Hoek.isPromise(1);
328+
Hoek.isPromise({});
329+
Hoek.isPromise(null);
330+
331+
expect.type<boolean>(Hoek.isPromise(1));
332+
333+
expect.error(Hoek.isPromise());
334+
expect.error(Hoek.isPromise(1, 2));
335+
336+
325337
// ts
326338

327339
interface X { a: number; };

0 commit comments

Comments
 (0)