<?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:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shlomi Nissan on Medium]]></title>
        <description><![CDATA[Stories by Shlomi Nissan on Medium]]></description>
        <link>https://medium.com/@shlominissan?source=rss-6193d6c4e4b0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*4pU6uljit_P0fpaS.jpeg</url>
            <title>Stories by Shlomi Nissan on Medium</title>
            <link>https://medium.com/@shlominissan?source=rss-6193d6c4e4b0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 18:50:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shlominissan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mistakes developers make using JavaScript Promises]]></title>
            <link>https://medium.com/@shlominissan/mistakes-developers-make-using-javascript-promises-7b01dfc164a1?source=rss-6193d6c4e4b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b01dfc164a1</guid>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-developer]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[vue]]></category>
            <dc:creator><![CDATA[Shlomi Nissan]]></dc:creator>
            <pubDate>Sun, 25 Nov 2018 22:03:06 GMT</pubDate>
            <atom:updated>2018-11-25T22:03:06.239Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*gE2BzzPpbnmq33k7HeVF8w.png" /></figure><p>Promises were introduced in ES6 to improve the way we handle asynchronous tasks in JavaScript. A promise is simply an object that serves as a placeholder for an asynchronous result but its implementation helps us solve three problems we experienced using simple callbacks:</p><p><strong>Difficult error handling: </strong>Since asynchronous callbacks are not being executed in the same step as the event loop, we can’t use built-in language constructs such as try/catch statements:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed08dd8aa2e449738951e23d15017619/href">https://medium.com/media/ed08dd8aa2e449738951e23d15017619/href</a></iframe><p><strong>Performing asynchronous code in parallel:</strong> In some cases, we want to perform an action preceded by asynchronous requests that are mutually independent. Running them in parallel is an opprtunity to improve performance but with simple callbacks, it requires some boilerplate code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e07fff3a62edc5de449443f245df2b0/href">https://medium.com/media/0e07fff3a62edc5de449443f245df2b0/href</a></iframe><p><strong>Performing asynchronous code in a sequence:</strong> If we have multiple asynchronous requests that <em>are </em>interdependent, we usually end up with code that’s difficult to read:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66add40e04ae7c778095086a73d0de92/href">https://medium.com/media/66add40e04ae7c778095086a73d0de92/href</a></iframe><p>The promise syntax immediately addresses the first problem, <em>difficult error handling</em>. The <em>then()</em> function lets us pass two callbacks to handle resolved promises, one for the fulfilled state (resolved successfully) and one for the rejected state (an error has occurred). As for the other two problems, some developers still fail to address them.</p><h3><strong>Mistake 1: Not running </strong>asynchronous tasks in parallel</h3><p>Promises offer useful functions that allow us to process asynchronous code in parallel but a lot of developers don’t take advantage of them, instead, they nest their promises and run their requests in a sequence:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3938b6f230581f7cd1f74a0badc9150/href">https://medium.com/media/a3938b6f230581f7cd1f74a0badc9150/href</a></iframe><p>In this example, the second promise will not be processed until the first one is resolved, but since they these requests are not mutually dependent, we should process them in parallel using <em>Promise.all():</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9e69fd1b3e7ae2c82b9355553766941/href">https://medium.com/media/f9e69fd1b3e7ae2c82b9355553766941/href</a></iframe><p>In this example, both promises will be processed asynchronously and only when both of them are resolved, we handle the result. JavaScript is still single-threaded but since fetch is a primitive handled internally by the browser, it supports multiple fetches at once.</p><p>Another related function is <em>Promise.race(). </em>Like <em>Promise.all()</em>, It takes an array of promises, but <em>Promise.race()</em> only handles the first request that’s resolved, regardless of which one it is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/545db6232fddfedbc7f962853f1039ba/href">https://medium.com/media/545db6232fddfedbc7f962853f1039ba/href</a></iframe><h3><strong>Mistake 2: Not chaining promises</strong></h3><p>The thing that makes the<em> then()</em> function special is, it always returns a promise. This allows us to chain promises and eliminates callback hell altogether. Unfortunately, nested promises are not uncommon:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c07a08525beca6335b54827b75b41a5c/href">https://medium.com/media/c07a08525beca6335b54827b75b41a5c/href</a></iframe><p>This example looks very similar to the one that’s using callbacks. Let’s see how we can make things a little better by chaining these requests:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5780a2f67c134dd47f9227c0e2bcb4b8/href">https://medium.com/media/5780a2f67c134dd47f9227c0e2bcb4b8/href</a></iframe><p>Looks much better! It certainly makes the code more readable, but we can do even better.</p><h3>Mistake 3: Not adopting new features</h3><p>Before we get into <em>async functions, </em>it’s worth mentioning they are not entirely new.</p><p>Promises were the most anticipated feature in ES6 but it wasn’t the only one that helped improve our experience with asynchronous code. Often overlooked, <em>generator functions</em> offered an interesting use case that helped us write asynchronous code that looked very much like synchronous code.</p><p>G<em>enerator functions</em> are special functions that can return multiple values in a sequence and on-demand. Use cases for generators are very limited in the context of a web app, however, combined with promises, generator functions offer a new way to write asynchronous code.</p><p>If we were to write an async handler function that takes a generator as a parameter, our last example would have looked like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d32580702195954efe57fb7669e1724b/href">https://medium.com/media/d32580702195954efe57fb7669e1724b/href</a></iframe><p>If you are curious about implementing an async handler using generators, I encourage you to explore this topic, but in the meantime, you can get started with async functions right away!</p><p>The ECMAScript committee was well aware of the benefits of combining generators and promises so they decided to save us the time by introducing <em>async functions</em> in ES2016.</p><p>Now, without writing any additional code, we could rewrite our last example using async/await:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e7027620bd5a2e64a318de9abc97042/href">https://medium.com/media/2e7027620bd5a2e64a318de9abc97042/href</a></iframe><p>It shouldn’t surprise you that both examples look very similar. Under the hood, async functions are essentially a mixture of generators and promises, but now we don’t have to think about the implementation.</p><p>If you want to learn more about async functions, a good place to start is <a href="https://developers.google.com/web/fundamentals/primers/async-functions">Async functions — making promises friendly</a> by Jake Archibald at Google.</p><p>I hope this article highlighted some common issues we can all avoid moving forward. If you’ve got feedback, I would love to hear about it so be sure to leave a comment. If you found this post useful 👏👏👏 away!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b01dfc164a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Object-oriented programming in vanilla JavaScript]]></title>
            <link>https://medium.com/hackernoon/object-oriented-programming-in-vanilla-javascript-f3945b15f08a?source=rss-6193d6c4e4b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3945b15f08a</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[vanilla-javascript]]></category>
            <dc:creator><![CDATA[Shlomi Nissan]]></dc:creator>
            <pubDate>Tue, 17 Oct 2017 03:25:58 GMT</pubDate>
            <atom:updated>2019-05-15T09:16:42.282Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*LdilJoQXl8aLfkXbxtiYZw.png" /></figure><p><a href="https://hackernoon.com/tagged/javascript">JavaScript</a> is a powerful object-oriented <a href="https://hackernoon.com/tagged/programming">programming</a> (OOP) language, however, unlike many traditional programming languages, it uses a prototype-based OOP model which makes its syntax foreign to most developers. In addition, JavaScript also treats functions as first-class objects which may cause further confusion amongst developers who are not familiar with these concepts.</p><p>It’s possible to side-step these concepts by adapting another programming language, such as TypeScript, which has a familiar syntax and offer additional features, however, such languages compile to plain JavaScript anyway so equipping yourself with this knowledge will not just help you understand how they work, but also when it’s suitable to use them.</p><p>Here’s a list of the topics we’re going to be covering:</p><p>Namespacing<br>Objects<br>Objects Literals<br>Construction Functions<br>Inheritance</p><h3>Namespacing</h3><p>With an ever-growing number of third-party libraries, frameworks and dependencies on the web, namespacing is imperative to JavaScript development as we try to avoid collisions between objects and variables in the global namespace.</p><p>Unfortunately, JavaScript doesn’t have built-in support for namespacing but we can use objects to achieve the same result. There are many different patterns to implementing a namespace in JavaScript but we’re going to cover nested namespacing, which is certainly the most common one.</p><p>The nested namespacing pattern uses an object literal to bundle up functionality under a unique, application specific name. We can start by creating a global object and assign it to a variable like this:</p><pre>var MyApp = MyApp || {};</pre><p>We can use same technique to create sub-namespaces as well:</p><pre>MyApp.users = MyApp.user || {};</pre><p>Once we’ve got a container, we can use it to define methods and properties and use them in our global namespace without the risking collision with existing definitions.</p><pre>MyApp.users = {</pre><pre>    // properties</pre><pre>    existingUsers: [...],</pre><pre>    // methods</pre><pre>    renderUsersHTML: function() {<br>      ...<br>    }</pre><pre>};</pre><p>An in-dept overview of namespacing patterns in JavaScript can be found here: <a href="https://addyosmani.com/blog/essential-js-namespacing/#beginners">Essential JavaScript Namespacing Patterns</a>.</p><h3>Objects</h3><p>If you have ever written code in JavaScript, you have been using objects at some capacity. JavaScript has three distinguishable types of objects:</p><h4>Native Objects</h4><p>Native objects are part of the language specification. They’re available to us regardless of the client our JavaScript code is running on. Examples of native objects would be: Array, Date, and Math. For a complete list, refer to <a href="https://mzl.la/2bTppcQ">JavaScript built-in objects reference</a></p><pre>var users = Array(); // Array is a native object</pre><h4>Host Objects</h4><p>Unlike native objects, host objects are made available by the client our JavaScript code is running on. Different clients have different host objects that allow us, in most cases, to interact with it. For example, if we’re writing code for a browser, it provides us with host objects such as: window, document, location and history.</p><pre>document.body.innerHTML = &#39;Hello&#39;; // document is a host object</pre><h4>User Objects</h4><p>User objects—sometimes referred to as contributed objects—are custom objects we define at run time. There are two ways to declare our own objects in JavaScript and we will cover them next.</p><h3>Object Literals</h3><p>We already touched object literals when we covered namespacing, but it’s time for a clear definition: An object literal is a comma-separated list of name-value pairs wrapped in curly braces. They can bundle up properties and methods, and like any other object in JavaScript, they can be passed to, and returned from functions. Here’s another example of an object literal:</p><pre>var dog = {</pre><pre>  // properties</pre><pre>  breed: ‘Bulldog’,</pre><pre>  // methods</pre><pre>  bark: function() {</pre><pre>    console.log(“Woof!”);</pre><pre>  },</pre><pre>};</pre><pre>// accessing methods and properties</pre><pre>dog.bark();</pre><p>Object literals are singletons. The most common use for them is encapsulating code and enclosing it in a tidy package to avoid collision with variables and objects on the global scope (namespacing), and pass configurations to plugins and objects.</p><p>Object literals are useful but they can’t be instantiated or inherited from. If we want to leverage these features, we need to explore another method of creating objects in JavaScript.</p><h3>Constructor Functions</h3><p>Functions in JavaScript are considered first-class citizens which means they support the same operations that are available to other entities. In JavaScript terms, this means that functions can be constructed at run-time, passed as arguments, returned from other functions, and be assigned to variables. Furthermore, they can have their own properties and methods. This allows us to use functions as objects that <em>can</em> be instantiated and inherited from.</p><p>Here’s an example of using defining an object using a constructor function:</p><pre>function User( name, email ) {</pre><pre>  // properties</pre><pre>  this.name = name;<br>  this.email = email;</pre><pre>  // methods</pre><pre>  this.sayHey = function() {</pre><pre>   console.log( “Hey, I’m “ + this.name );</pre><pre>  };</pre><pre>}</pre><pre>// instantiating the object</pre><pre>var steve = new User( “Steve”, “steve@hotmail.com” );</pre><pre>// accessing methods and properties</pre><pre>steve.sayHey();</pre><p>Creating a constructor function is similar to creating a regular function with one exception: we’re using the this keyword to declare properties and methods.</p><p>Instantiating constructor functions using the new keyword is similar to instantiating objects in traditional class-based programming languages, however, there is one problem that may not be apparent at first.</p><p>When we’re creating new objects using the new keyword in JavaScript, we’re running the function block again and again which causes our script to declare anonymous functions for each method, EVERY TIME. This will cause our program to consume more memory then it should, and it could have serious implications on performance, depending on the scale of your program.</p><p>Luckily, there’s a better way to attach methods to constructor functions without polluting the global scope.</p><h4>Methods and Prototypes</h4><p>JavaScript is a prototypal programming language, which means we can use prototypes as templates for objects. This will help us avoid the anonymous functions trap as we scale our applications. prototype is a special property in JavaScript that let’s us add new methods to an object.</p><p>Here’s a rewrite of our previous example using prototypes:</p><pre>function User( name, email ) {</pre><pre>  // properties</pre><pre>  this.name = name;<br>  this.email = email;</pre><pre>}</pre><pre>// methods</pre><pre>User.prototype.sayHey = function() {</pre><pre>  console.log( “Hey, I’m “ + this.name );</pre><pre>}</pre><pre>// instantiating the object</pre><pre>var steve = new User( “Steve”, “steve@hotmail.com” );</pre><pre>// accessing methods and properties</pre><pre>steve.sayHey();</pre><p>In this example, sayHey() will be shared through all instances of the User object.</p><h3>Inheritance</h3><p>Prototypes are also used for inheritance through the prototype chain. In JavaScript, every object has a prototype, and since a prototype is just another object, it has a prototype as well, and so on… until we reach a prototype with a value of null—the final link in the prototype chain.</p><p>When we access a method or property, JavaScript checks if it’s defined within the object definition, if it isn’t, it will check the prototype and see if it’s defined there. If it can’t find it there either, it will keep going down the prototype chain until it’s found, or until it reaches the end of the chain.</p><p>Here’s how it works:</p><pre>// the user object</pre><pre>function User( name, email, role ) {</pre><pre>  this.name = name;<br>  this.email = email;<br>  this.role = role;</pre><pre>}</pre><pre>User.prototype.sayHey = function() {</pre><pre>  console.log( “Hey, I’m an “ + role);</pre><pre>}</pre><pre>// the editor object inherits from user</pre><pre>function Editor( name, email ) {</pre><pre>   // The Call function is calling the Constructor of User<br>   // and decorates Editor with the same properties</pre><pre>   User.call(this, name, email, &quot;admin&quot;); </pre><pre>}</pre><pre>// To set up the prototype chain, we create a new object using<br>// the User prototype and assign it to the Editor prototype</pre><pre>Editor.prototype = Object.create( User.prototype );</pre><pre>// Now we can access all the properties and methods<br>// of User from the Editor object</pre><pre>var david = new Editor( &quot;David&quot;, &quot;matthew@medium.com&quot; );</pre><pre>david.sayHey();</pre><p>Prototypal inheritance may take some time getting used to, but it’s an important concept to grasp in order to master OOP in vanilla JavaScript. While it is often considered to be one of JavaScript’s weaknesses, the prototypal inheritance model is in fact more powerful than the classic model. It is, for example, fairly trivial to build a classic inheritance model on top of a prototypal model.</p><p><em>ECMAScript 6 introduced a new set of keywords implementing </em><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><em>classes</em></a><em>. Although these constructs looks like class-based languages, they are not the same. JavaScript remains prototype-based.</em></p><p>JavaScript has evolved over a long period of time, and during this time different practices that should be avoided by today’s standard have been adopted by a variety of developers. With the introduction of ES2015, this is slowly starting to change, however, many developers still stick to their old ways which compromises the relevancy of their code. Understanding and applying OOP programming in JavaScript is imperative to writing sustainable code, and I hope this brief introduction will help you achieve that.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fdde502%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Fhackernoon%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href">https://medium.com/media/3c851dac986ab6dbb2d1aaa91205a8eb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f3945b15f08a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/object-oriented-programming-in-vanilla-javascript-f3945b15f08a">Object-oriented programming in vanilla JavaScript</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>