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){


javascript node-js

Bootstrap 5 Complete Course with Examples

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

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

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).

Hire Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.

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.

Hands on with Node.Js Streams | Examples & Approach

The practical implications of having Streams in Node.js are vast. Nodejs Streams are a great way to handle data chunks and uncomplicate development.