close

How do I tell if an object is a Promise?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How do I tell if an object is a Promise? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How do I tell if an object is a Promise ?

  1. How do I tell if an object is a Promise ?

    do I tell if an object is a Promise Checking if something is promise unnecessarily complicates the code, just use Promise.resolve
    Promise.resolve(valueOrPromiseItDoesntMatter).then(function(value) { })

  2. do I tell if an object is a Promise

    do I tell if an object is a Promise Checking if something is promise unnecessarily complicates the code, just use Promise.resolve
    Promise.resolve(valueOrPromiseItDoesntMatter).then(function(value) { })

Method 1

Checking if something is promise unnecessarily complicates the code, just use Promise.resolve

Promise.resolve(valueOrPromiseItDoesntMatter).then(function(value) {

})

Method 2

after searching for a reliable way to detect Async functions or even Promises, i ended up using the following test :

() => fn.constructor.name === 'Promise' || fn.constructor.name === 'AsyncFunction'

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment