DEV Community

Cover image for Unveiling the Magic of Promises with `is.promise` and `is.not_promise` from 'thiis'
Ivan Karbashevskyi
Ivan Karbashevskyi

Posted on

Unveiling the Magic of Promises with `is.promise` and `is.not_promise` from 'thiis'

JavaScript is an enchanting realm filled with promises, and not just the kind you make to your code but the special kind that handles asynchronous operations. In this magical journey, we'll unravel the secrets of promises using the is.promise and is.not_promise methods from the 'thiis' package. Get ready to embark on an adventure into the world of JavaScript promises!

Embracing the World of Promises

Image description

Before we dive into the magic, let's understand what promises are in JavaScript. Promises are a way to handle asynchronous operations, providing a cleaner alternative to callbacks. They represent a value that may be available now, or in the future, or never. Asynchronous tasks like fetching data from an API or reading a file often involve promises.

Meet is.promise - Your Promise Whisperer

Documentation link

Imagine you're in a world full of promises, and you need a guide to help you distinguish them. That's where the is.promise method comes in. It's like a magical whisperer that tells you whether a value is indeed a promise. Let's witness its magic:

import { is } from 'thiis'; // Import the "is" object from the "thiis" package

const myPromise = fetch('');
const result = is.promise(myPromise);

console.log(result); // true
Enter fullscreen mode Exit fullscreen mode

In this example, we import the "is" object from the "thiis" package and use the is.promise method to confirm that myPromise is indeed a promise. As expected, it returns true because the value is indeed a promise.

The Magic Unveiled: Examples Galore!

Now, let's explore a series of enchanting examples that showcase the versatility of is.promise and its companion, is.not_promise. We'll encounter scenarios involving promises in various shapes and sizes.

1. Chasing Async Dreams with is.promise

The primary role of is.promise is to identify promises. It's your compass in the realm of asynchronous operations:

import { is } from 'thiis';

const asyncOperation = someAsyncFunction();

if (is.promise(asyncOperation)) {
  // Embrace the asynchronous magic!
} else {
  // Continue with other synchronous tasks.
Enter fullscreen mode Exit fullscreen mode

2. Guardian of Non-Promises - is.not_promise

Conversely, is.not_promise serves as your guardian against non-promises. It ensures that a value isn't a promise before proceeding:

import { is } from 'thiis';

const importantValue = someFunctionThatShouldNotReturnAPromise();

if (is.not_promise(importantValue)) {
  // Your guardian ensures a smooth path.
} else {
  // Time to explore other possibilities.
Enter fullscreen mode Exit fullscreen mode

3. Promising User Interactions

Promises often play a role in user interactions, especially when dealing with asynchronous actions. Validate promises with is.promise:

import { is } from 'thiis';

function performAsyncTask() {
  return new Promise(resolve => {
    // Simulating an asynchronous task
    setTimeout(() => {
      resolve('Task completed!');
    }, 1000);

const userAction = getUserInput();

if (is.promise(userAction)) {
  // User is ready for asynchronous adventures!
} else {
  // Handle other types of user actions.
Enter fullscreen mode Exit fullscreen mode

4. Magical Streams with is.promise

In the enchanting world of streams, you might encounter promises. Use filter and is.promise to ensure that only promise values are part of your stream:

import { is } from 'thiis';
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';

const stream$ = from([Promise.resolve('Magical Promise'), 'not a promise', 42]);

  .subscribe(value => {
    console.log(value); // Only the magical promise will join the stream!
Enter fullscreen mode Exit fullscreen mode

Here, the filter(is.promise) ensures that only promise values are part of the stream.

5. Array Adventures with is.not_promise

Arrays can also be magical, but sometimes you want to ensure they don't contain promises. Use some and is.not_promise for this magical check:

import { is } from 'thiis';

const nonPromiseArray = [1, 'not a promise', true];
const promiseArray = [Promise.resolve('Magical Promise'), 'not magical', 42];

const hasNoPromises = nonPromiseArray.some(is.not_promise); // true
const hasPromises = promiseArray.some(is.not_promise); // false

Enter fullscreen mode Exit fullscreen mode

Here, hasNoPromises checks if there are no promises in nonPromiseArray, and hasPromises checks if there are promises in promiseArray.

6. Promise or Not? Let's Check!

Sometimes you just want to know if something is a promise or not. Use is.promise for a straightforward check:

import { is } from 'thiis';

const mysteriousThing = getSomeMysteriousValue();

if (is.promise(mysteriousThing)) {
  // It's a promise! The mystery unfolds.
} else {
  // The mystery remains unsolved.
Enter fullscreen mode Exit fullscreen mode

The Enchanting Conclusion

The is.promise and is.not_promise methods from the 'thiis' package are your magical companions in the world of JavaScript promises. They make handling asynchronous operations playful and precise, ensuring your code interacts with promises exactly as intended. By adding the 'thiis' package to your JavaScript toolkit and exploring its documentation for more tips and examples, you can weave magical spells in your asynchronous adventures.

So, continue coding, and may your promises always resolve!

🎗 ChatGPT & DALL·E 3

Top comments (1)

karbashevskyi profile image
Ivan Karbashevskyi

Telegram channel: