Handle exceptions within a returned promise

Handle exceptions within a returned promise

I know this is not the most beautiful code, but due to legacy issues, I need to stick to this workflow.

I know this is not the most beautiful code, but due to legacy issues, I need to stick to this workflow.

The problem is that I am not able to bubble up any exceptions that might occur in the heart of the returned promise.

The code is designed that both the reject and resolve return valid data. So, if you change the const CONDITION to 0.4, we will be getting a rejection. If the value of const CONDITION stays at 0.6, we will be getting a resolution. This works so far.

However, in cases where we have structural failures, such as in the example below where we try to pass a wrong variable name into the rejection:

let reasoxxxx = '__FAILED__';
reject({error: reason, data: output});

I am not able to invoke a throw to bubble up the error. For that reason, I am getting the usual ugly message and I cannot bubble up a proper exception:

Exchange request was rejected due to error(s).
(node:224) UnhandledPromiseRejectionWarning: ReferenceError: reason is not defined
(node:224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:224) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Any ideas? The code snipped should work.

function fakeFetch() {
  // Promisify the request.
  return new Promise((resolve, reject) => {
    // Emulate an asynchroneous fetch.
    setTimeout(() => {
      let result = 0.4; // Change to 0.4 to trigger a failed fetch.
      if (result < 0.5) {;
        reject('__FAIL__');
      } else {
        resolve({name: 'apple', price: '1234.12', time: 1549926859970});
      }
    }, 2000);
  });
}

async function sendExchangeRequest(id, pair, symbols, callback) { let err, result await fakeFetch().then((output) => { result = output }).catch((error) => {err = error}) if(err){ result = 'None' }else{ err = 'None' } callback(err, result) }

async function fetchExchangeData(id, pair, symbols) { // Promisify the request. try { return new Promise((resolve, reject) => { try { // Send the request. sendExchangeRequest(id, pair, symbols, ((err, output) => { try{ if(err){


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Node.js for Beginners - Learn Node.js from Scratch (Step by Step)

Node.js for Beginners - Learn Node.js from Scratch (Step by Step) - Learn the basics of Node.js. This Node.js tutorial will guide you step by step so that you will learn basics and theory of every part. Learn to use Node.js like a professional. You’ll learn: Basic Of Node, Modules, NPM In Node, Event, Email, Uploading File, Advance Of Node.

How to Use Express.js, Node.js and MongoDB.js

In this post, I will show you how to use Express.js, Node.js and MongoDB.js. We will be creating a very simple Node application, that will allow users to input data that they want to store in a MongoDB database. It will also show all items that have been entered into the database.