<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[DevBlog by Ricardo Dantas]]></title><description><![CDATA[How-tos, thoughts, and tips about web development.]]></description><link>http://devblog.ricardodantas.me/</link><image><url>http://devblog.ricardodantas.me/favicon.png</url><title>DevBlog by Ricardo Dantas</title><link>http://devblog.ricardodantas.me/</link></image><generator>Ghost 3.2</generator><lastBuildDate>Sun, 16 Aug 2020 08:46:31 GMT</lastBuildDate><atom:link href="http://devblog.ricardodantas.me/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Javascript: How to convert an array of objects to an object]]></title><description><![CDATA[<!--kg-card-begin: markdown--><pre><code class="language-javascript">const arrayToObject = (array, keyField) =&gt;
   array.reduce((obj, item) =&gt; {
     return {...obj, [item[keyField]]: item}
   }, {});

const peopleArray = [{id: 1, name: &quot;John&quot;}, {id:2, name: &quot;Robert&quot;}]

const peopleObject = arrayToObject(peopleArray, &quot;id&quot;);

console.log(peopleObject); // {&quot;1&quot;:{&quot;id&quot;:1,&quot;name&quot;:&quot;</code></pre>]]></description><link>http://devblog.ricardodantas.me/how-to-convert-array-t/</link><guid isPermaLink="false">5ea5a7597213d3001ec2c528</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Thu, 30 Apr 2020 07:05:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><pre><code class="language-javascript">const arrayToObject = (array, keyField) =&gt;
   array.reduce((obj, item) =&gt; {
     return {...obj, [item[keyField]]: item}
   }, {});

const peopleArray = [{id: 1, name: &quot;John&quot;}, {id:2, name: &quot;Robert&quot;}]

const peopleObject = arrayToObject(peopleArray, &quot;id&quot;);

console.log(peopleObject); // {&quot;1&quot;:{&quot;id&quot;:1,&quot;name&quot;:&quot;John&quot;},&quot;2&quot;:{&quot;id&quot;:2,&quot;name&quot;:&quot;Robert&quot;}}
</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Javascript: How to copy to clipboard]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>Javascript:</p>
<pre><code class="language-javascript">function myFunction() {
  /* Get the text field */
  var copyText = document.getElementById(&quot;myInput&quot;);

  /* Select the text field */
  copyText.select();
  copyText.setSelectionRange(0, 99999); /*For mobile devices*/

  /* Copy the text inside the text field */
  document.execCommand(&quot;copy&quot;);

  /* Alert the copied text */
  alert(&quot;Copied the text: &quot; + copyText.</code></pre>]]></description><link>http://devblog.ricardodantas.me/javascript-how-to-copy-to-clipboard/</link><guid isPermaLink="false">5ea5a0ec7213d3001ec2c518</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Wed, 29 Apr 2020 16:50:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>Javascript:</p>
<pre><code class="language-javascript">function myFunction() {
  /* Get the text field */
  var copyText = document.getElementById(&quot;myInput&quot;);

  /* Select the text field */
  copyText.select();
  copyText.setSelectionRange(0, 99999); /*For mobile devices*/

  /* Copy the text inside the text field */
  document.execCommand(&quot;copy&quot;);

  /* Alert the copied text */
  alert(&quot;Copied the text: &quot; + copyText.value);
}
</code></pre>
<!--kg-card-end: markdown--><hr><!--kg-card-begin: markdown--><p>HTML:</p>
<pre><code class="language-html">&lt;!-- The text field --&gt;
&lt;input type=&quot;text&quot; value=&quot;Hello World&quot; id=&quot;myInput&quot;&gt;

&lt;!-- The button used to copy the text --&gt;
&lt;button onclick=&quot;myFunction()&quot;&gt;Copy text&lt;/button&gt;
</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Javascript: How to replace all occurrences of a string]]></title><description><![CDATA[<!--kg-card-begin: markdown--><pre><code class="language-javascript">String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

&quot;my string should be replace here and here&quot;.replaceAll(&quot;here&quot;,&quot;replaced match&quot;) // &quot;my string should be replace replaced match and replaced match&quot;
</code></pre>
<!--kg-card-end: markdown-->]]></description><link>http://devblog.ricardodantas.me/how-to-replace-all-occurrences-of-a-string/</link><guid isPermaLink="false">5ea59e927213d3001ec2c4fc</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Wed, 29 Apr 2020 16:23:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><pre><code class="language-javascript">String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

&quot;my string should be replace here and here&quot;.replaceAll(&quot;here&quot;,&quot;replaced match&quot;) // &quot;my string should be replace replaced match and replaced match&quot;
</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[JavaScript: How to randomize (shuffle) an array]]></title><description><![CDATA[<!--kg-card-begin: markdown--><pre><code class="language-javascript">function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;</code></pre>]]></description><link>http://devblog.ricardodantas.me/how-to-randomize-an-array-in-javascript/</link><guid isPermaLink="false">5ea59d637213d3001ec2c4f1</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Tue, 28 Apr 2020 07:05:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><pre><code class="language-javascript">function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

// How to use the function
var arr = [2, 11, 37, 42];
shuffle(arr);
console.log(arr);
</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[JavaScript: How to validate an email address]]></title><description><![CDATA[<!--kg-card-begin: markdown--><pre><code class="language-javascript">function validateEmail(email) {
    var re = /^(([^&lt;&gt;()\[\]\\.,;:\s@&quot;]+(\.[^&lt;&gt;()\[\]\\.,;:\s@&quot;]+)*)|(&quot;.+&quot;))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(String(email).toLowerCase());
}
</code></pre>
<!--kg-card-end: markdown-->]]></description><link>http://devblog.ricardodantas.me/how-to-validate-an-email-address-in-javascript/</link><guid isPermaLink="false">5ea59c607213d3001ec2c4e6</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Mon, 27 Apr 2020 19:06:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><pre><code class="language-javascript">function validateEmail(email) {
    var re = /^(([^&lt;&gt;()\[\]\\.,;:\s@&quot;]+(\.[^&lt;&gt;()\[\]\\.,;:\s@&quot;]+)*)|(&quot;.+&quot;))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(String(email).toLowerCase());
}
</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[10 Best Javascript books for 2020]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>Here is my pick of the 10 JavaScript books that web developer of any skill level, including aspirants, need to read in 2020:</p>
<ol>
<li>
<p><a href="https://amzn.to/3bFD6aL">A Smarter Way to Learn JavaScript: The new tech-assisted approach that requires half the effort</a></p>
</li>
<li>
<p><a href="https://amzn.to/3aAUdJH">Eloquent JavaScript: A Modern Introduction to Programming</a></p>
</li>
<li>
<p><a href="https://amzn.to/3bE5UjT">JavaScript: The Good Parts</a></p>
</li>
<li>
<p><a href="https://amzn.to/2y3jizF">Learn</a></p></li></ol>]]></description><link>http://devblog.ricardodantas.me/10-best-javascript-books-for-2020/</link><guid isPermaLink="false">5ea596ee7213d3001ec2c4c4</guid><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Thu, 23 Apr 2020 14:17:00 GMT</pubDate><media:content url="https://res-1.cloudinary.com/hetlwsket/image/upload/q_auto/v1/ghost-blog-images/claudia-wolff-MiJTU6lqksg-unsplash.jpg" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><img src="https://res-1.cloudinary.com/hetlwsket/image/upload/q_auto/v1/ghost-blog-images/claudia-wolff-MiJTU6lqksg-unsplash.jpg" alt="10 Best Javascript books for 2020"><p>Here is my pick of the 10 JavaScript books that web developer of any skill level, including aspirants, need to read in 2020:</p>
<ol>
<li>
<p><a href="https://amzn.to/3bFD6aL">A Smarter Way to Learn JavaScript: The new tech-assisted approach that requires half the effort</a></p>
</li>
<li>
<p><a href="https://amzn.to/3aAUdJH">Eloquent JavaScript: A Modern Introduction to Programming</a></p>
</li>
<li>
<p><a href="https://amzn.to/3bE5UjT">JavaScript: The Good Parts</a></p>
</li>
<li>
<p><a href="https://amzn.to/2y3jizF">Learn JavaScript VISUALLY</a></p>
</li>
<li>
<p><a href="https://amzn.to/3558zRr">JavaScript: The Definitive Guide</a></p>
</li>
<li>
<p><a href="https://amzn.to/3cPsbvr">Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript</a></p>
</li>
<li>
<p><a href="https://amzn.to/2W13SDU">JavaScript for Kids: A Playful Introduction to Programming</a></p>
</li>
<li>
<p><a href="https://amzn.to/3567SHr">Programming JavaScript Applications: Robust Web Architecture with Node, HTML5, and Moderns JS Libraries</a></p>
</li>
<li>
<p><a href="https://amzn.to/3aGlpXn">High-Performance Browser Networking</a></p>
</li>
<li>
<p><a href="https://amzn.to/2Y1Wuee">Functional Programming in JavaScript: How to Improve Your JavaScript Programs Using Functional Techniques</a></p>
</li>
</ol>
<h2 id="bonus">Bonus:</h2>
<ul>
<li><a href="https://amzn.to/3cNXuqj">You Don’t Know JS book series</a></li>
</ul>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Javascript: How to format a date]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>For custom-delimited date formats, you have to pull out the date (or time) components from a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/format">DateTimeFormat object</a> (which is part of the ECMAScript Internationalization API), and then manually create a string with the delimiters you want.</p>
<p>To do this, you can use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/formatToParts">DateTimeFormat#formatToParts</a>:</p>
<pre><code class="language-javascript">const d = new Date('2010-08-05')</code></pre>]]></description><link>http://devblog.ricardodantas.me/how-to-format-a-javascript-date/</link><guid isPermaLink="false">5ea564bc0f8d1c001eee7110</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Wed, 22 Apr 2020 10:38:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>For custom-delimited date formats, you have to pull out the date (or time) components from a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/format">DateTimeFormat object</a> (which is part of the ECMAScript Internationalization API), and then manually create a string with the delimiters you want.</p>
<p>To do this, you can use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/formatToParts">DateTimeFormat#formatToParts</a>:</p>
<pre><code class="language-javascript">const d = new Date('2010-08-05')
const dtf = new Intl.DateTimeFormat('en', { year: 'numeric', month: 'short', day: '2-digit' }) 
const [{ value: month },,{ value: day },,{ value: year }] = dtf.formatToParts(d) 

console.log(`${day}-${month}-${year}`)
</code></pre>
<p>You can also pull out the parts of a DateTimeFormat one-by-one using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/format">DateTimeFormat#format</a>, but note that when using this method, as of March 2020, there is a bug in the ECMAScript implementation when it comes to leading zeros on minutes and seconds (this bug is circumvented by the approach above).</p>
<pre><code class="language-javascript">const d = new Date('2010-08-05')
const year = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d)
const month = new Intl.DateTimeFormat('en', { month: 'short' }).format(d)
const day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d)

console.log(`${day}-${month}-${year}`)
</code></pre>
<p>When working with dates and times, it is usually worth using a library (eg. <a href="https://momentjs.com/">moment.js</a>) because of the many hidden complexities of the field.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Javascript: How to access and process nested objects, arrays or JSON]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>Given this object:</p>
<pre><code class="language-javascript">const data = {
    code: 42,
    items: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        name: 'bar'
    }]
};
</code></pre>
<p>Let's assume we want to access the name of the second item.</p>
<p>Here is how we can do it step-by-step:</p>
<p>As we can see data is an object, hence we can access its properties</p>]]></description><link>http://devblog.ricardodantas.me/how-to-access-and-process-nested-objects-arrays-or-json/</link><guid isPermaLink="false">5ea5636a0f8d1c001eee70f3</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Tue, 21 Apr 2020 10:33:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>Given this object:</p>
<pre><code class="language-javascript">const data = {
    code: 42,
    items: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        name: 'bar'
    }]
};
</code></pre>
<p>Let's assume we want to access the name of the second item.</p>
<p>Here is how we can do it step-by-step:</p>
<p>As we can see data is an object, hence we can access its properties using dot notation. The items property is accessed as follows:</p>
<pre><code class="language-javascript">data.items
</code></pre>
<p>The value is an array, to access its second element, we have to use bracket notation:</p>
<pre><code class="language-javascript">data.items[1]
</code></pre>
<p>This value is an object and we use dot notation again to access the name property. So we eventually get:</p>
<pre><code class="language-javascript">const item_name = data.items[1].name;
</code></pre>
<p>Alternatively, we could have used bracket notation for any of the properties, especially if the name contained characters that would have made it invalid for dot notation usage:</p>
<pre><code class="language-javascript">const item_name = data['items'][1]['name'];
</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Javascript: How to dynamically access object property using variable]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>There are two ways to access properties of an object:</p>
<h2 id="1dotnotation">1. Dot notation</h2>
<pre><code class="language-javascript">const person1 = {
  firstname: 'John',
  lastname: 'Doe'
};
console.log(person1.firstname) //John 
</code></pre>
<!--kg-card-end: markdown--><hr><!--kg-card-begin: markdown--><h2 id="2bracketnotation">2. Bracket notation</h2>
<p>The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use</p>]]></description><link>http://devblog.ricardodantas.me/javascript-how-to-dynamically-access-object-property-using-variable/</link><guid isPermaLink="false">5ea55d170f8d1c001eee70c2</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Mon, 20 Apr 2020 10:06:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>There are two ways to access properties of an object:</p>
<h2 id="1dotnotation">1. Dot notation</h2>
<pre><code class="language-javascript">const person1 = {
  firstname: 'John',
  lastname: 'Doe'
};
console.log(person1.firstname) //John 
</code></pre>
<!--kg-card-end: markdown--><hr><!--kg-card-begin: markdown--><h2 id="2bracketnotation">2. Bracket notation</h2>
<p>The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:</p>
<pre><code class="language-javascript">const person1 = {
  firstname: 'John',
  lastname: 'Doe'
};
console.log(person1['firstname']) //John 
</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Javascript: How to get query string values]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>You can use <a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#Browser_compatibility">URLSearchParams</a>.</p>
<pre><code class="language-javascript">const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');
</code></pre>
<!--kg-card-end: markdown-->]]></description><link>http://devblog.ricardodantas.me/javascript-how-to-get-query-string-values/</link><guid isPermaLink="false">5ea55bd20f8d1c001eee70ae</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Fri, 17 Apr 2020 10:00:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>You can use <a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#Browser_compatibility">URLSearchParams</a>.</p>
<pre><code class="language-javascript">const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');
</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Javascript: How to use async/await with a forEach loop]]></title><description><![CDATA[<!--kg-card-begin: markdown--><h2 id="readinginsequence">Reading in sequence</h2>
<p>If you want to read the files in sequence, you cannot use forEach indeed. Just use a modern for … of loop instead, in which await will work as expected:</p>
<pre><code class="language-javascript">async function printFiles () {
  const files = await getFilePaths();

  for (const file of files) {
    const contents = await fs.readFile(file,</code></pre>]]></description><link>http://devblog.ricardodantas.me/how-to-use-async-await-with-a-foreach-loop/</link><guid isPermaLink="false">5ea55a240f8d1c001eee707e</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Thu, 16 Apr 2020 09:53:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><h2 id="readinginsequence">Reading in sequence</h2>
<p>If you want to read the files in sequence, you cannot use forEach indeed. Just use a modern for … of loop instead, in which await will work as expected:</p>
<pre><code class="language-javascript">async function printFiles () {
  const files = await getFilePaths();

  for (const file of files) {
    const contents = await fs.readFile(file, 'utf8');
    console.log(contents);
  }
}
</code></pre>
<!--kg-card-end: markdown--><hr><!--kg-card-begin: markdown--><h2 id="readinginparallel">Reading in parallel</h2>
<p>If you want to read the files in parallel, you cannot use <code>forEach</code>. Each of the <code>async</code> callback function calls does return a promise, but you're throwing them away instead of awaiting them. Just use <code>map</code> instead, and you can await the array of promises that you'll get with <code>Promise.all</code>:</p>
<pre><code class="language-javascript">async function printFiles () {
  const files = await getFilePaths();

  await Promise.all(files.map(async (file) =&gt; {
    const contents = await fs.readFile(file, 'utf8')
    console.log(contents)
  }));
}
</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Javascript: How to convert an existing callback API to promises]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>Promise returning functions should never throw, they should return rejections instead. Throwing from a promise returning function will force you to use both a } catch { and a .catch. People using promisified APIs do not expect promises to throw.</p>
<pre><code class="language-javascript">// Before promises
function divisionAPI (number, divider, successCallback, errorCallback) {
    if (divider == 0){
        return</code></pre>]]></description><link>http://devblog.ricardodantas.me/javascript-how-to-convert-an-existing-callback-api-to-promises/</link><guid isPermaLink="false">5ea558320f8d1c001eee7069</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Wed, 15 Apr 2020 09:45:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>Promise returning functions should never throw, they should return rejections instead. Throwing from a promise returning function will force you to use both a } catch { and a .catch. People using promisified APIs do not expect promises to throw.</p>
<pre><code class="language-javascript">// Before promises
function divisionAPI (number, divider, successCallback, errorCallback) {
    if (divider == 0){
        return errorCallback( new Error(&quot;Division by zero&quot;) )
    }    
        
    successCallback( number / divider )
}


// After promises
function divisionAPI (number, divider) {

    return new Promise(function (fulfilled, rejected) {
        if (divider == 0) {
            return rejected( new Error(&quot;Division by zero&quot;) )
        }
        fulfilled( number / divider )
     })
}
</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Javascript: How to access the correct this inside a callback]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p><code>this</code> (&quot;the context&quot;) is a special keyword inside each function and its value only depends on how the function was called, not how/when/where it was defined. It is not affected by lexical scopes like other variables (except for arrow functions, see below). Here are some possibilities:</p>]]></description><link>http://devblog.ricardodantas.me/how-to-access-the-correct-this-inside-a-callback/</link><guid isPermaLink="false">5ea554b20f8d1c001eee7035</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Tue, 14 Apr 2020 09:28:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p><code>this</code> (&quot;the context&quot;) is a special keyword inside each function and its value only depends on how the function was called, not how/when/where it was defined. It is not affected by lexical scopes like other variables (except for arrow functions, see below). Here are some possibilities:</p>
<pre><code class="language-javascript">
// 1. Using bind method
function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', ( function () {
        alert(this.data);
    }).bind(this) );
}
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};
var obj = new MyConstructor('foo', transport);


// 2. Storing reference to context/this inside another variable
function MyConstructor(data, transport) {
  var self = this;
  this.data = data;
  transport.on('data', function() {
    alert(self.data);
  });
}


// 3.Arrow function
function MyConstructor(data, transport) {
  this.data = data;
  transport.on('data', () =&gt; {
    alert(this.data);
  });
}

</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Javascript: How to return the response from an asynchronous call]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>While certain asynchronous operations provide synchronous counterparts (so does &quot;Ajax&quot;), it's generally discouraged to use them, especially in a browser context.</p>
<p>There are three different solutions that are all building on top of each other:</p>
<ol>
<li><strong>Promises with async/await</strong> (ES2017+, available in older browsers if you use a</li></ol>]]></description><link>http://devblog.ricardodantas.me/how-to-return-the-response-from-an-asynchronous-call/</link><guid isPermaLink="false">5ea5448304d832001eefdc7e</guid><category><![CDATA[How to: Javascript]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Mon, 13 Apr 2020 08:49:00 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>While certain asynchronous operations provide synchronous counterparts (so does &quot;Ajax&quot;), it's generally discouraged to use them, especially in a browser context.</p>
<p>There are three different solutions that are all building on top of each other:</p>
<ol>
<li><strong>Promises with async/await</strong> (ES2017+, available in older browsers if you use a transpiler or regenerator)</li>
<li><strong>Promises with then()</strong> (ES2015+, available in older browsers if you use one of the many promise libraries)</li>
<li><strong>Callbacks</strong> (aka <a href="http://callbackhell.com/">Callback Hell</a>, not recommended )</li>
</ol>
<!--kg-card-end: markdown--><hr><!--kg-card-begin: markdown--><pre><code class="language-javascript">function getGithubRepository() {
  return new Promise(function (resolve, reject) {
    $.ajax({
      method: &quot;get&quot;,
      url: &quot;https://api.github.com/repos/ricardodantas/heroku-scripts&quot;
    })
      .done(function (response) {
        resolve(response);
      })
      .fail(function (error) {
        reject(error);
      });
  });
}

// Promise with then()
getGithubRepository()
  .then(function (result) {
    console.log(&quot;Then: Repo name: &quot;, result.name);
  })
  .catch(function (error) {
    console.error(error.statusText);
  });

// Promises with async/await
(async () =&gt; {
  try {
    const result = await getGithubRepository();
    console.log(&quot;Async/Await Repo name: &quot;, result.name);
  } catch (err) {
    console.log(err);
  }
})();</code></pre>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Why am I embracing minimalism?]]></title><description><![CDATA[<p>I think I had this connection with minimalism since I started to realize myself as an adult person. I always tried to get rid of things that were bothering my focus on reach my goals, I just didn’t know there was a term for that. Then Sweden happened in</p>]]></description><link>http://devblog.ricardodantas.me/why-am-i-embracing-minimalism/</link><guid isPermaLink="false">5ea4ae2e1a4def001e8a7e1d</guid><category><![CDATA[Lifestyle]]></category><dc:creator><![CDATA[Ricardo Dantas]]></dc:creator><pubDate>Fri, 10 Apr 2020 07:40:00 GMT</pubDate><media:content url="https://res-2.cloudinary.com/hetlwsket/image/upload/q_auto/v1/ghost-blog-images/Minimalism_Pic.jpg" medium="image"/><content:encoded><![CDATA[<img src="https://res-2.cloudinary.com/hetlwsket/image/upload/q_auto/v1/ghost-blog-images/Minimalism_Pic.jpg" alt="Why am I embracing minimalism?"><p>I think I had this connection with minimalism since I started to realize myself as an adult person. I always tried to get rid of things that were bothering my focus on reach my goals, I just didn’t know there was a term for that. Then Sweden happened in my life. I started to realize that people here were different, it seems they can take decisions easily and they seem so light in terms of balance their lives. So I started to dig in about the Swedish culture and this word, "minimalism", began to show up so often that it took my attention, and I began to research it. I ended up finding <a href="https://www.netflix.com/se-en/title/80114460">this documentary</a> on Netflix. I watched it, and it was mind-blowing! I finally connected all points in my mind. For years I was doing many minimalist things to my lifestyle without knowing that term. It was crazy.</p><h2 id="what-is-minimalism">What is minimalism?</h2><p>Minimalism is a tool that can assist you in finding freedom. Freedom from fear. Freedom from worry. Freedom from overwhelm. Freedom from guilt. Freedom from depression. Freedom from the trappings of the consumer culture we’ve built our lives around. Real freedom. - <a href="https://www.theminimalists.com/minimalism/">theminimalists.com</a></p><p>It doesn't make sense to describe the full concept here, this is not the idea of this post, but if you are interested in learning more, I advise you to start on <a href="https://www.theminimalists.com/start/">theminimalists.com/start</a>.</p><p>I also recommend you to read the following books:</p><ul><li><a href="https://amzn.to/3dVLfcC">Everything That Remains: A Memoir</a> (by The Minimalists)</li><li><a href="https://amzn.to/34iowTN">Minimalism: Live a Meaningful Life</a> (by Joshua Fields Millburn and Ryan Nicodemus)</li><li><a href="https://amzn.to/3bRzXnZ">Digital Minimalism: Choosing a Focused Life in a Noisy World</a> (by Cal Newport)</li></ul><h2 id="so-why">So why?</h2><p>It's simple! I want to be rich! No, I am not only talking about this kind($) of richness. I am talking about having more time to enjoy life with people I really want to, doing things I really want to and turn my decision process simpler.</p><p>The minimalist approach helps me to reach that. Having fewer things to worry about, fewer things to consume and fewer things to demand my actions or decisions. Then I will start to create an environment that has no dependencies, fears of losing stuff or that sense that I need more than 24 hours a day to complete my tasks. I have to say, it might be difficult for some people but you can do it if you really want to.</p><p>I am even incorporating the minimalist approach in my professional career and it has been great so far (I will detail more about it in the next posts).</p><h2 id="how-did-i-start">How did I start?</h2><p>This is how I did; I've started with the most basics.</p><p>Getting rid of everything I don't really need or used just once (clothes, electronics, material stuff in general);</p><p>Getting rid of everything that consumes your time and makes you don't produce anything valuable (time limit to interact on social media - about 20 min during the day);</p><p>Saying "no" for things that I'm not interested in.</p><p>With only these points I managed to get more time to myself, save money, read more books, start out this blog and start to plan my own podcast.</p><h2 id="conclusion">Conclusion</h2><p>I am not saying you should get rid of everything as you might read on someone else's posts, but I am saying that you can get rid of everything that bothers you, sucks your life and time, and doesn't bring value to you (material things, people, jobs, etc.). To say "no" is not that bad; it's part of the game, you have to choose your path and control your life instead of outsourcing your decision to destiny. I think minimalism is more a thing you incorporate in yourself and develop along the time.</p><p>I will keep the focus on this journey, sharing my experiences through my social media profiles and here on this blog. Feel free to follow me on <a href="https://instagram.com/ricardodantas">Instagram</a> and <a href="https://twitter.com/ricardodantas">Twitter</a>.</p>]]></content:encoded></item></channel></rss>