Node.js util.types.isPromise() Method
The util.types.isPromise() method is an inbuilt application programming interface of the util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isPromise() method is used to determine whether the value is a built-in Promise.
Syntax:
util.types.isPromise( value )
Parameter: This method accepts a single parameters value that holds any valid JavaScript data types like Boolean, Null, Number, Object, etc.
Return value: It returns a Boolean value i.e. returns true if the value is a built-in Promise otherwise it returns false.
The below examples illustrate the use of util.types.isPromise() method in Node.js:
Example 1:
javascript
// Node.js program to demonstrate the // util.types.isPromise() method // Using require to access util module const util = require( 'util' ); // Using util.types.isPromise() method console.log(util.types.isPromise( true )); // Using util.types.isPromise() method console.log(util.types.isPromise( new Set())); // Using util.types.isPromise() method console.log(util.types.isPromise( new Promise( function (resolve, reject) { }))); |
Output:
false false true
Example 2:
javascript
// Node.js program to demonstrate the // util.types.isPromise() method // Using require to access util module const util = require( 'util' ); // Using util.types.isPromise() method console.log(util.types.isPromise( true )); // Using util.types.isPromise() method console.log(util.types.isPromise( new Set())); // Defining a Promise const promise = new Promise( function (resolve, reject) { const x = "w3wiki" ; const y = "w3wiki" if (x === y) { resolve(); } else { reject() } }); // Using util.types.isPromise() method console.log(util.types.isPromise(promise. then( function () { console.log( 'Success, You are a GEEK' ); }). catch ( function () { console.log( 'Some error has occurred' ); }) )); |
Output:
false false true Success, You are a GEEK
Note: The above program will compile and run by using the node index.js command.
Reference: https://nodejs.org/dist/latest-v13.x/docs/api/util.html#util_util_types_ispromise_value
Contact Us