<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Benjamin Raffetseder</title>
    <description>The latest articles on DEV Community by Benjamin Raffetseder (@benjaminraffetseder).</description>
    <link>https://dev.to/benjaminraffetseder</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F107475%2F0c90931d-c78f-4963-8e9c-0e5cd814d5d2.jpg</url>
      <title>DEV Community: Benjamin Raffetseder</title>
      <link>https://dev.to/benjaminraffetseder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/benjaminraffetseder"/>
    <language>en</language>
    <item>
      <title>How To Keep Track Of Responses In NestJS</title>
      <dc:creator>Benjamin Raffetseder</dc:creator>
      <pubDate>Sun, 25 Aug 2024 16:16:25 +0000</pubDate>
      <link>https://dev.to/benjaminraffetseder/how-to-keep-track-of-responses-in-nestjs-1dnh</link>
      <guid>https://dev.to/benjaminraffetseder/how-to-keep-track-of-responses-in-nestjs-1dnh</guid>
      <description>&lt;p&gt;In backend development, effective logging is essential for maintaining a healthy and efficient system. It provides insights into the behavior of your application, aiding in debugging, troubleshooting, and performance optimization. NestJS offers a way to implement custom middleware, including logging mechanisms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing the Response Middleware
&lt;/h2&gt;

&lt;p&gt;In this blog post, we will explore how to create a response logging middleware in NestJS. This middleware will log the incoming request, the response status, and the response time. By implementing this middleware, you can keep track of the requests and responses in your application, which is essential for monitoring and debugging.&lt;/p&gt;

&lt;p&gt;Here's an example of a response logging middleware in NestJS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;NestMiddleware&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NextFunction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ResponseMiddleware&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;NestMiddleware&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Response&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * This middleware logs the request and response status code in the console.
   * The format of the log is:
   * &amp;lt;METHOD&amp;gt; &amp;lt;URL&amp;gt; &amp;lt;STATUS CODE&amp;gt; &amp;lt;RESPONSE TIME IN MS&amp;gt;
   *
   * @param {Request} req - The incoming request
   * @param {Response} res - The outgoing response
   * @param {NextFunction} next - The next middleware in the stack
   */&lt;/span&gt;
  &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NextFunction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;originalUrl&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;requestStartTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;getTime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * This event is emitted when the response is finished.
     * We log the request and response information in this listener.
     */&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;finish&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;statusCode&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;responseTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;getTime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;responseTime&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;requestStartTime&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;originalUrl&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;ms`&lt;/span&gt;

      &lt;span class="cm"&gt;/**
       * Log the request and response information in the console.
       */&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * Call the next middleware in the stack.
     */&lt;/span&gt;
    &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How It Works
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;ResponseMiddleware&lt;/code&gt; class implements the &lt;code&gt;NestMiddleware&lt;/code&gt; interface, which requires the implementation of a &lt;code&gt;use&lt;/code&gt; method. This method is called for each incoming request, and it takes three parameters: &lt;code&gt;req&lt;/code&gt; (the incoming request), &lt;code&gt;res&lt;/code&gt; (the outgoing response), and &lt;code&gt;next&lt;/code&gt; (the next middleware in the stack).&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;use&lt;/code&gt; method, we extract the HTTP method and the URL from the incoming request. We also record the start time of the request using &lt;code&gt;new Date().getTime()&lt;/code&gt;. We then attach an event listener to the &lt;code&gt;finish&lt;/code&gt; event of the response object. This event is emitted when the response is finished and ready to be sent back to the client.&lt;/p&gt;

&lt;p&gt;When the &lt;code&gt;finish&lt;/code&gt; event is triggered, we calculate the response time by subtracting the start time from the current time. We then construct a log message containing the HTTP method, URL, status code, and response time. Finally, we log this message using the &lt;code&gt;Logger&lt;/code&gt; class provided by NestJS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Enhanced Monitoring&lt;/strong&gt;: By logging detailed information about each request and response, you can monitor your application's performance and identify slow endpoints.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplified Debugging&lt;/strong&gt;: The logs provide valuable insights into the behavior of your application, making it easier to debug issues and understand the flow of requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance Optimization&lt;/strong&gt;: By analyzing the response times, you can identify bottlenecks and optimize the performance of your application.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Registering the Middleware
&lt;/h2&gt;

&lt;p&gt;To use this middleware in your NestJS application, you need to register it in the appropriate module. Here's how you can register the &lt;code&gt;ResponseMiddleware&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MiddlewareConsumer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;NestModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ResponseMiddleware&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./response.middleware&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppModule&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;NestModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;configure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MiddlewareConsumer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ResponseMiddleware&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;forRoutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Apply the middleware to all routes&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In conclusion, implementing a response logging middleware in NestJS is a simple yet powerful way to enhance the monitoring and debugging capabilities of your backend. &lt;/p&gt;

&lt;p&gt;You could extend this middleware to log additional information such as request headers, response headers, and request body or even integrate it with 3rd-party logging services.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>nestjs</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Simplifying Async Error Handling in TypeScript</title>
      <dc:creator>Benjamin Raffetseder</dc:creator>
      <pubDate>Fri, 23 Aug 2024 10:30:06 +0000</pubDate>
      <link>https://dev.to/benjaminraffetseder/simplifying-async-error-handling-in-typescript-26g2</link>
      <guid>https://dev.to/benjaminraffetseder/simplifying-async-error-handling-in-typescript-26g2</guid>
      <description>&lt;p&gt;If you've worked with asynchronous code in TypeScript for any length of time, you'll know that error handling can get messy fast. Between nested try-catch blocks and repetitive error-handling logic, it doesn't take long before your codebase starts feeling cluttered. That's why I've found myself leaning toward a more elegant solution — the &lt;code&gt;tryCatch&lt;/code&gt; helper function. This little utility is inspired by Go's error-handling approach and wraps your async functions in a way that simplifies error management, making your code more readable and maintainable.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Code
&lt;/h3&gt;

&lt;p&gt;Let's jump right into the &lt;code&gt;tryCatch&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tryCatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;logError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;logError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why I Use This Approach
&lt;/h3&gt;

&lt;p&gt;As someone who's spent a lot of time dealing with asynchronous code, I've come to really appreciate the benefits &lt;code&gt;tryCatch&lt;/code&gt; brings to the table:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cleaner Code&lt;/strong&gt;: One of the biggest advantages is that &lt;code&gt;tryCatch&lt;/code&gt; helps reduce the number of try-catch blocks scattered throughout your code. This may seem like a small thing, but when you're managing multiple async operations, it makes a big difference in keeping your code readable and manageable. Instead of having multiple try-catch blocks everywhere, you deal with both success and error cases in a predictable, clean way. And as a bonus, this approach makes it easier for others to read your code and understand what's going on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consistent Error Handling&lt;/strong&gt;: One thing that I've learned over the years is that consistent error handling is key to a maintainable codebase. With &lt;code&gt;tryCatch&lt;/code&gt;, you establish a clear and uniform pattern for handling errors across your code. This not only reduces mental overhead but also makes your codebase easier to maintain as it scales.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optional Logging&lt;/strong&gt;: Sometimes you want to log errors right away, sometimes you don't. The optional &lt;code&gt;logError&lt;/code&gt; flag gives you the flexibility to toggle error logging on or off, depending on what makes sense for the situation. This keeps your code free from repetitive console logs calls and allows for centralized logging behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Type Safety&lt;/strong&gt;: Since we're working with TypeScript, type safety is always on my mind. &lt;code&gt;tryCatch&lt;/code&gt; ensures that if an error occurs, the result will be undefined, and vice versa. This reduces the chances of those frustrating runtime bugs that crop up because of type mismatches. Plus, it's a nice reassurance when you're juggling complex data flows.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Example Usage:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;tryCatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;// Your async logic here&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;// Handle the error&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt; 
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Handle the absence of data&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="c1"&gt;// Continue with the data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why It's Better Than Traditional &lt;code&gt;try-catch&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;I've seen plenty of projects where traditional try-catch blocks work well—at first. But as the project grows and async operations multiply, they start to become a headache. Here's how &lt;code&gt;tryCatch&lt;/code&gt; can make things easier:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Deep Nesting&lt;/strong&gt;: Once you start nesting multiple &lt;code&gt;try-catch&lt;/code&gt; blocks, it's easy to lose track of what's happening where. &lt;code&gt;tryCatch&lt;/code&gt; helps keep your code flatter, which makes the logic easier to follow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Single Responsibility&lt;/strong&gt;: This function allows you to encapsulate error handling in a single place. I like to think of it as aligning with the single responsibility principle — your async functions focus on what they're supposed to do, while tryCatch handles the errors. This separation makes the code easier to read and maintain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Focus on the Happy Path&lt;/strong&gt;: One of my favorite things about using &lt;code&gt;tryCatch&lt;/code&gt; is how it allows me to focus on the happy path in my code. Instead of constantly breaking the flow to deal with errors, I can keep my main logic straightforward and handle the errors separately. This small shift can make a big difference when you're trying to keep the overall intent of your code clear.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;As someone who's been building with TypeScript for a while now, I've found &lt;code&gt;tryCatch&lt;/code&gt; to be a real asset in managing asynchronous error handling. It's one of those small changes that have a surprisingly big impact, especially when you're working in a large or growing codebase. By reducing noise and ensuring consistent error handling, &lt;code&gt;tryCatch&lt;/code&gt; helps me write cleaner, more maintainable code.&lt;/p&gt;

&lt;p&gt;If you haven't tried it yet, give it a go — it might just make your life a little easier too.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>tutorial</category>
      <category>learning</category>
      <category>webdev</category>
    </item>
    <item>
      <title>I just launched my first real side project!</title>
      <dc:creator>Benjamin Raffetseder</dc:creator>
      <pubDate>Thu, 19 Sep 2019 15:27:45 +0000</pubDate>
      <link>https://dev.to/benjaminraffetseder/just-launched-my-first-real-side-project-208e</link>
      <guid>https://dev.to/benjaminraffetseder/just-launched-my-first-real-side-project-208e</guid>
      <description>&lt;p&gt;First of all, hi dev.to community!&lt;/p&gt;

&lt;p&gt;This is my first post, I've been here a little longer but actually just spent the most of the time lurking.&lt;/p&gt;

&lt;p&gt;As I mentioned above I just "finished" my side project called "&lt;a href="https://www.deliciously.cooking" rel="noopener noreferrer"&gt;deliciously.cooking&lt;/a&gt;". It's a mix between a traditional recipe site and Tinder.&lt;/p&gt;

&lt;p&gt;I know that sometimes finding something that you actually want to cook can be a big struggle if you get a big overview of all recipes. That's why designed &amp;amp; developed this site. You just a random selection of cards and if you see what you like you can get further information about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's under the hood?
&lt;/h2&gt;

&lt;p&gt;The site runs on Drupal and Vue. The explore page gets the recipes via an API using Vue to fetch and display them. &lt;/p&gt;

&lt;p&gt;Users are able to register and upload own recipes over a simple form. They're also able to save other recipes to their account.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is this project done?
&lt;/h2&gt;

&lt;p&gt;No, it's far away from being finished, at least in my opinion! I still have a lot of ideas which I want to add. (Also some bugs to fix)&lt;/p&gt;

&lt;p&gt;I also need to promote and get users to the platform. Maybe you have interest in it and have a look?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.deliciously.cooking" rel="noopener noreferrer"&gt;deliciously.cooking&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sideprojects</category>
      <category>showdev</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
