title: Propagate rejected promise via a .catch()
body: <p>I have some code which essentially looks like this:</p>
  
  <pre><code>export function firstFunction(req: express.Request, res: express.Response, next: express.NextFunction): void {
    secondFunction(id)
    .then((userId: UserId) =&gt; {
      res.status(200).send(UserId);
    })
    .catch((err) =&gt; {
      if (err instanceof NoResultError) {
        res.status(404).send(err);
      } else {
        next(err);
      }
    });
  }
  
  
  export function secondFunction(id: string): Promise&lt;UserId&gt; {
    return new Promise&lt;UserId&gt;((resolve, reject) =&gt; {
      thirdFunction(id)
      .then((data: TableInfo) =&gt; {
        if (Object.keys(data).length !== 3) {
          reject(new Error('data in database is not mapped properly'));
        }
        resolve(data);
      })
      .catch((err) =&gt; {
        // WANT TO PROPAGATE ERROR UP TO THE GETDETAILS FUNCTION WHICH CALLS THIS
      });
    });
  }
  
  export function thirdFunction(id: string): Promise&lt;TableInfo&gt; {
    return new Promise&lt;TableInfo&gt;((resolve, reject) =&gt; {
      let query = `
          //query goes here
      `;
      db.executeQuery(query, [id])
      .then((data: TableInfo) =&gt; {
        if (Object.keys(data).length &lt; 1) {
          reject(new NoResultError('some message here'));
        }
        resolve(data);
      });
    });
  }
  </code></pre>
  
  <p>My goal is to have the lowest level of the three functions (thirdFunction) determine if the data from the db-query finds no data and then reject that promise with an error. Then the secondFunction should ideally catch this error and propagate it up to firstFunction so that firstFunction can handle that error properly. I have tried doing a <code>throw err</code> a <code>return err</code> and a <code>return Promise.reject(err)</code> all of which lead to an unhandled promise rejection. What is my (probably fundamental) misunderstanding of how this should work?</p>