Javascript Promise Reject | Promise.reject() in Javascript

Javascript Promise Reject | Promise.reject() in Javascript

Javascript Promise reject() is an inbuilt function that returns the Promise object that is rejected with a given reason. Javascript promise reject example.

Javascript Promise reject() is an inbuilt function that returns the Promise object that is rejected with a given reason. The static Promise.reject() function returns the Promise that is rejected. For debugging purposes and selective error catching, it is useful to make reason an instanceof Error.

Javascript Promise Reject Example

The syntax for Javascript Promise.reject() method is following.

Promise.reject(reason);

The parameter reason is why this promise rejected.

Let us take a simple example.

// app.js

const p1 = new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
    setTimeout(() => {
        reject('fail promise');
    }, 1000);
});

p1.catch(error => { 
    console.log(error);
});

Here, we are rejecting the promise, and then we call the catch method on that promise to catch the error and log that error in the console.

javascript promise.reject instanceof

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

JavaScript Promise: Methods Comparison

What are promises and what is the difference between Promise.all, Promise.allSettled, Promise.race and Promise.any?

Javascript Instanceof Operator Example | Instanceof in Javascript

Javascript instanceof is an inbuilt operator that tests whether a prototype property of the constructor appears anywhere in the prototype chain of an object.

Javascript Instanceof Operator Example | Instanceof in Javascript

Javascript instanceof is an inbuilt operator that tests whether a prototype property of the constructor appears anywhere in the prototype chain of an object.

A Gentle Introduction to JavaScript Promises. I PromiseĀ :).

Promises are a fairly new API in JavaScript so we will first look at how asynchronous operations were done before promises. The most common way was to pass in callback functions that were called and ran after the success or failure of the asynchronous function.

Promise.allSettled() vs Promise.all()

Promise.allSetlled() is recently introduced in ECMA 2020. Check out how it is different from Promise.all().What is the difference between Promise.all() and Promise.allSetlled() ?