{"path": "/posts/building-pipelines-and-avoid-suffering/", "title": "building pipelines and avoid suffering · Bruno Dias", "html": "<!doctype html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <meta name=\"description\" content=\"building pipelines and avoid suffering\">\n  <title>building pipelines and avoid suffering · Bruno Dias</title>\n  <link rel=\"canonical\" href=\"https://diasbruno.github.io/posts/building-pipelines-and-avoid-suffering/\" data-post-metadata>\n  <meta name=\"author\" content=\"Bruno Dias\" data-post-metadata>\n  <meta property=\"og:type\" content=\"article\" data-post-metadata>\n  <meta property=\"og:title\" content=\"building pipelines and avoid suffering\" data-post-metadata>\n  <meta property=\"og:url\" content=\"https://diasbruno.github.io/posts/building-pipelines-and-avoid-suffering/\" data-post-metadata>\n  <meta property=\"article:published_time\" content=\"2023-12-30T22:34:58Z\" data-post-metadata>\n  <meta property=\"og:image\" content=\"https://diasbruno.github.io/images/promises.png\" data-post-metadata>\n  <link rel=\"icon\" href=\"/assets/site/signature.svg\" type=\"image/svg+xml\">\n  <link rel=\"alternate\" type=\"application/atom+xml\" title=\"diasbruno\" href=\"/atom.xml\">\n  <link rel=\"alternate\" type=\"application/json\" href=\"/posts/building-pipelines-and-avoid-suffering/content.json\">\n  <link rel=\"stylesheet\" href=\"/assets/site/tokens.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/site.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/footer.css\">\n\n</head>\n<body class=\"light\">\n<div id=\"site-page\">\n<a class=\"skip-link\" href=\"#content\">Skip to content</a>\n<header class=\"site-header\">\n  <a class=\"brand\" href=\"/\" data-site-navigation aria-label=\"Bruno Dias, home\">\n    <img src=\"/assets/site/signature.svg\" width=\"24\" height=\"34\" alt=\"\">\n  </a>\n  <nav aria-label=\"Main navigation\">\n    <ul>\n<li><a class=\"nav-vrn\" href=\"/vrn/\" data-site-navigation><span class=\"nav-label\">vrn</span> <span class=\"nav-badge\">research</span></a></li>\n<li><a class=\"nav-posts\" href=\"/posts/\" data-site-navigation aria-current=\"location\">Posts</a></li>\n<li><a class=\"nav-projects\" href=\"/projects/\" data-site-navigation>Projects</a></li>\n<li><a class=\"nav-about\" href=\"/about/\" data-site-navigation>About</a></li>\n</ul>\n  </nav>\n</header>\n\n<main class=\"section\" id=\"content\" tabindex=\"-1\">\n  <article class=\"post-detail\">\n    <header class=\"section-header\">\n      <p class=\"post-category\">Post</p>\n      <h1>building pipelines and avoid suffering</h1>\n\n      <p><time datetime=\"2023-12-30T22:34:58Z\">2023-12-30</time></p>\n    </header>\n    <div class=\"post-columns\">\n      <section class=\"post-body\" aria-label=\"Article\">\n\n<p>Before we start making amazing stuff with pipelines,\nlet's recap everything about promises - I mean everything!</p>\n<p><code>Promise</code> is an utility to execute tasks concurrently with\nthe main execution.</p>\n<h2>1 - Creating promises</h2>\n<p>There are 2 ways to create a promise:</p>\n<h3>new Promise</h3>\n<p>The constructor, <code>Promise(f)</code> receivers a function\nthat will be executed in a scheduled manner.</p>\n<p>In other to combine more promises,\nthis function <code>f</code> receives 2 callbacks: <code>resolve</code>\nand <code>reject</code>.</p>\n<p>By calling one of thoses 2 callback,\nit may call a continuation, if defined,\nusing <code>Promise.then(f)</code>, in case of <code>resolve</code> gets called\nor <code>Promise.catch(g)</code> for <code>reject</code>.</p>\n<h4>Creating a promise that has no continuations</h4>\n<pre><code class=\"language-js\">new Promise(() =&gt; patchUser({ id: 1, updateName: &quot;new name&quot; }))\n</code></pre>\n<h4>Creating a promise with continuation</h4>\n<pre><code class=\"language-js\">new Promise(\n  (resolve, reject) =&gt;\n    (Boolean(Math.ceil(Math.random() - 0.5))\n        ? resolve\n        : reject)()\n).then(\n  () =&gt; console.log(&quot;resolved&quot;)\n).catch(\n  () =&gt; console.log(&quot;rejected&quot;)\n)\n</code></pre>\n<h3>Lifting a value into promise</h3>\n<p>As the promise constructor argument function\nreceives 2 callbacks, they are also available\nto initialize a promise.</p>\n<pre><code class=\"language-js\">Promise.resolve(\n  1\n).then(\n  (value) =&gt; assert(value === 1)\n)\n\nPromise.reject(\n  { error: &quot;some error&quot; }\n).catch(\n  (err) =&gt; assert(error == &quot;some error&quot;)\n)\n</code></pre>\n<h2>2 - Continuations</h2>\n<p>You may think you know which path the execution will take,\nbut you need to pay attention to every continuation.</p>\n<h3><code>Promise.then(f)</code></h3>\n<p>Addind a <code>.then(f)</code> continuation, if the promise\nwas fulfilled and the result is <code>resolve</code>,\nthe continuation is triggered calling the function <code>f</code>\nwith the result passed to resolve. If the promise calls\n<code>reject</code>, than the continuation is not triggered.</p>\n<pre><code class=\"language-js\">Promise.resolve(\n  1\n).then(\n  (value) =&gt; assert(value === 1)\n)\n\nPromise.reject(\n  { error: &quot;some error&quot; }\n).then(\n  () =&gt; null /* function will not be called */\n)\n</code></pre>\n<h4>Return from <code>.then(f)</code></h4>\n<p>You can return 3 values from <code>.then(f)</code>:</p>\n<ul>\n<li>Any object</li>\n</ul>\n<p>If you return a normal object from it,\nit is the same as calling <code>resolve(object)</code>.\nThis will trigger the next &quot;then&quot; continuation,\nif defined.</p>\n<pre><code class=\"language-js\">Promise.resolve(\n  1\n).then(\n  (value) =&gt; value + 1\n).then(\n  (value) =&gt; assert(value === 2)\n)\n</code></pre>\n<ul>\n<li><code>Promise.resolve(object)</code></li>\n</ul>\n<p>You can return a promise from a continuation,\nin the case of returning &quot;resolved&quot; promise\nfrom a &quot;then&quot; continuation, it has the same effect\nas return a normal object.</p>\n<pre><code class=\"language-js\">Promise.resolve(\n  1\n).then(\n  (value) =&gt; Promise.resolve(value + 1)\n).then(\n  (value) =&gt; assert(value === 2)\n)\n</code></pre>\n<ul>\n<li><code>Promise.reject(object)</code></li>\n</ul>\n<p>As you can return a promise,\nit can also be a &quot;rejected&quot; one.</p>\n<p>This will trigger a &quot;catch&quot; continuation.</p>\n<pre><code class=\"language-js\">Promise.resolve(\n  1\n).then(\n  (value) =&gt; Promise.reject({ error: &quot;some error&quot; })\n).catch(\n  ({ error }) =&gt; assert(error == &quot;some error&quot;)\n)\n</code></pre>\n<h3><code>Promise.catch(g)</code></h3>\n<p>Adding a <code>.catch(g)</code> continuation, whenever\na promise is fulfilled and &quot;rejected&quot;,\nthe function <code>g</code> will be called with the object\nof <code>reject(object)</code>.</p>\n<pre><code class=\"language-js\">Promise.reject(\n  { error: &quot;some error&quot; }\n).then(\n  () =&gt; null /* function will not be called */\n).catch(\n  ({ error }) =&gt; assert(error == &quot;some error&quot;)\n)\n</code></pre>\n<h4>Return from <code>.catch(g)</code></h4>\n<h5>Return resolved promises or normal objects</h5>\n<p>Return from catch is a little different,\nbut interesting in some cases, and,\nthere are a lot of interesting stuff\nto do with it.</p>\n<ul>\n<li><code>Promise.resolve(object)</code></li>\n</ul>\n<p>By returning a &quot;resolved&quot; promise from a catch\ncontinuation, the next continuation to be &quot;resolved&quot;\nand the <code>.then(f)</code> will be triggered.</p>\n<pre><code class=\"language-js\">Promise.reject(\n  { error: &quot;some error&quot; }\n).catch(\n  () =&gt; Promise.resolve(&quot;ok&quot;)\n).catch(\n  () =&gt; null /* function will not be called */\n).then(\n  (value) =&gt; assert(value == &quot;ok&quot;)\n)\n</code></pre>\n<ul>\n<li>Any object</li>\n</ul>\n<p>Returning a normal value from a &quot;catch&quot;\ncontinuation has the same behavior\nas returning a <code>Promise.resolve(object)</code>.</p>\n<pre><code class=\"language-js\">Promise.reject(\n  { error: &quot;some error&quot; }\n).catch(\n  () =&gt; &quot;ok&quot;\n).catch(\n  () =&gt; null /* function will not be called */\n).then(\n  (value) =&gt; assert(value == &quot;ok&quot;)\n)\n</code></pre>\n<h4>Use cases for return resolved promises or normal objects</h4>\n<p>In some pipelines, there are cases there it may be possible\nto recover and continue the pipeline.</p>\n<p>Example (default values):</p>\n<pre><code class=\"language-js\">Promise.resolve(\n    /* user recovered from localStorage */\n  user\n).then(\n    /* we are going to kick out this user,\n       so we are going to reject here */\n  (user) =&gt; validateUserSession(user)\n).catch(\n    /* we can return a object that simulates\n       a user but with almost no privileges\n       (better then use null) */\n  () =&gt; GuestUser()\n).then(\n  (user) =&gt; assert(user.isAuthenticated() === false)\n)\n</code></pre>\n\n      </section>\n      <aside class=\"post-links\" aria-labelledby=\"post-links-heading\">\n        <h2 id=\"post-links-heading\">Links</h2>\n        <ul>\n<li><a href=\"/posts/\" data-site-navigation>All posts</a></li>\n        </ul>\n      </aside>\n    </div>\n  </article>\n</main>\n\n<footer class=\"site-footer\">\n  <div class=\"site-footer-inner\">\n    <a class=\"site-footer-home\" href=\"/\" data-site-navigation>Bruno Dias</a>\n    <ul aria-label=\"Footer links\">\n      <li><a href=\"https://github.com/diasbruno\">GitHub</a></li>\n      <li><a href=\"/atom.xml\">Atom feed</a></li>\n    </ul>\n  </div>\n</footer>\n\n</div>\n<script type=\"module\" src=\"/assets/site/navigation.js\"></script>\n</body>\n</html>\n"}
