diff --git a/README.md b/README.md index d980e1337..82c18d2cf 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,7 @@ * [`isFunction`](#isfunction) * [`isNull`](#isnull) * [`isNumber`](#isnumber) +* [`isPromiseLike`](#ispromiselike) * [`isString`](#isstring) * [`isSymbol`](#issymbol) * [`isValidJSON`](#isvalidjson) @@ -4196,6 +4197,37 @@ isNumber(1); // true
[⬆ Back to top](#table-of-contents) +### isPromiseLike + +Returns `true` if an object looks like a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), `false` otherwise. + +Check if the object is not `null`, its `typeof` matches either `object` or `function` and if it has a `.then` property, which is also a `function`. + +```js +const isPromiseLike = obj => + obj !== null && + (typeof obj === 'object' || typeof obj === 'function') && + typeof obj.then === 'function'; +``` + +
+Examples + +```js +isPromiseLike({ + then: function() { + return ''; + } +}); // true +isPromiseLike(null); // false +isPromiseLike({}); // false +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### isString Checks if the given argument is a string. diff --git a/docs/index.html b/docs/index.html index 9571d93b4..22e389e92 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -876,6 +876,17 @@ isNull('null'); // false
 

isNumber

Checks if the given argument is a number.

Use typeof to check if a value is classified as a number primitive.

const isNumber = val => typeof val === 'number';
 
isNumber('1'); // false
 isNumber(1); // true
+

isPromiseLike

Returns true if an object looks like a Promise, false otherwise.

Check if the object is not null, its typeof matches either object or function and if it has a .then property, which is also a function.

const isPromiseLike = obj =>
+  obj !== null &&
+  (typeof obj === 'object' || typeof obj === 'function') &&
+  typeof obj.then === 'function';
+
isPromiseLike({
+  then: function() {
+    return '';
+  }
+}); // true
+isPromiseLike(null); // false
+isPromiseLike({}); // false
 

isString

Checks if the given argument is a string.

Use typeof to check if a value is classified as a string primitive.

const isString = val => typeof val === 'string';
 
isString(10); // false
 isString('10'); // true
diff --git a/snippets/isPromiseLike.md b/snippets/isPromiseLike.md
index d7bb9a75b..6a2ae29d4 100644
--- a/snippets/isPromiseLike.md
+++ b/snippets/isPromiseLike.md
@@ -6,11 +6,17 @@ Check if the object is not `null`, its `typeof` matches either `object` or `func
 
 ```js
 const isPromiseLike = obj =>
-  obj !== null && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
+  obj !== null &&
+  (typeof obj === 'object' || typeof obj === 'function') &&
+  typeof obj.then === 'function';
 ```
 
 ```js
-isPromiseLike({then:function () {return ''}}); // true
+isPromiseLike({
+  then: function() {
+    return '';
+  }
+}); // true
 isPromiseLike(null); // false
 isPromiseLike({}); // false
 ```