<?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 Sveta Buben on Medium]]></title>
        <description><![CDATA[Stories by Sveta Buben on Medium]]></description>
        <link>https://medium.com/@svetabuben?source=rss-24428562d375------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2YWrP1YSbGc26sVQ.</url>
            <title>Stories by Sveta Buben on Medium</title>
            <link>https://medium.com/@svetabuben?source=rss-24428562d375------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 12:34:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@svetabuben/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[JavaScript: Understanding generators]]></title>
            <link>https://medium.com/@svetabuben/javascript-understanding-generators-8ba64326360f?source=rss-24428562d375------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ba64326360f</guid>
            <category><![CDATA[technical-literacy]]></category>
            <category><![CDATA[js-generator]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sveta Buben]]></dc:creator>
            <pubDate>Tue, 25 Feb 2020 23:39:20 GMT</pubDate>
            <atom:updated>2020-02-26T09:10:52.925Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*lwm0Zf3CjJgkx8awknlgfg.jpeg" /><figcaption><a href="https://i.pinimg.com/564x/3d/61/bf/3d61bf0062e488ae5a1fb83c4b1f8395.jpg">Objects of common interest</a></figcaption></figure><p>A complete introduction into what generators in JS are, with examples are walkthrough to what is happening under the hood.</p><ol><li><a href="https://medium.com/@svetabuben/understanding-generators-in-js-part-1-b0fe2d983cf5">Getting next element iteratively</a></li><li><a href="https://medium.com/@svetabuben/understanding-generators-in-js-part-2-getting-next-element-with-a-generator-function-51ae3e954cd9">Getting the next element with a generator function</a></li><li><a href="https://medium.com/@svetabuben/understanding-generators-in-js-part-3-pass-data-dynamically-to-a-generator-function-70b860508c99">Pass data dynamically to a generator function</a></li><li><a href="https://medium.com/@svetabuben/understanding-generators-in-js-part-4-async-generators-4a01f82ceb5d">Async generators</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ba64326360f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Generators in JS. Part 4 (async generators)]]></title>
            <link>https://medium.com/@svetabuben/understanding-generators-in-js-part-4-async-generators-4a01f82ceb5d?source=rss-24428562d375------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a01f82ceb5d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[js-generator]]></category>
            <dc:creator><![CDATA[Sveta Buben]]></dc:creator>
            <pubDate>Sat, 25 Jan 2020 04:53:10 GMT</pubDate>
            <atom:updated>2020-02-25T23:14:34.871Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part 4: async generators</h3><p>This is a continuation of<strong> Understanding Generators</strong> series. In previous articles I have discussed what is happening under the hood when we invoke a generator function as well how to pass data to a generator function dynamically.</p><p>Take a look at this code snippet.</p><pre><br>function doWhenDataReceived(value) {</pre><pre>  returnNextElement.next()</pre><pre>}</pre><pre>function* createFlow() {</pre><pre>  const data = yield fetch(‘https://...&#39;);</pre><pre>  console.log(data);</pre><pre>}</pre><pre>const returnNextElement = createFlow();</pre><pre>const futureData = returnNextElement.next();</pre><pre>futureData.then(doWhenDataReceived);</pre><p>And this is a diagram that explains what is happening under the hood when JS runs this code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Ftoy3CY1eX0AWNFIh1wqw.png" /></figure><p>There is a great explanation for points 1,2,3,6 in my previous articles of this series. However, it is worth mentioning that when we call <em>fetch</em> in step 4, two things are happening under the hood.</p><p>1) In JS it returns a promise object with properties ( the one we are interested in) <em>value</em> and <em>onFulfillment</em>. <em>value</em> is <em>undefined</em> at the beginning, but it is populated with a value once a promise is resolved (here at 200ms promise is resolved with value ‘hi’). <em>onFulfillment</em> in an empty array at the beginning, but method <em>.then</em> adds a function (here <em>doWhenDataReceived</em>) to the onFulfillment that will be invoked once value property is assigned to some value (here at 200ms promise is resolved with value ‘hi’ and assign it to futureData.value) ( I am omitting a process of Event Loop and Microtask queue here for brevity)</p><p>2) Also, it speaks to Web Browser API feature XmlHttpRequest, which speaks to a server and returned value assigned to the value property of <em>futureData</em> object.</p><p>Great, leave your comment if you found this a bit confusing for you or share was it helpful for you 💖</p><p>Next article will talk about <em>async await</em></p><p>Resources:<br>☛ <a href="https://frontendmasters.com/courses/javascript-new-hard-parts/">https://frontendmasters.com/courses/javascript-new-hard-parts/</a> — by Will Sentance</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a01f82ceb5d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Generators in JS. Part 3 (pass data dynamically to a generator function)]]></title>
            <link>https://medium.com/@svetabuben/understanding-generators-in-js-part-3-pass-data-dynamically-to-a-generator-function-70b860508c99?source=rss-24428562d375------2</link>
            <guid isPermaLink="false">https://medium.com/p/70b860508c99</guid>
            <category><![CDATA[generator]]></category>
            <category><![CDATA[iterators]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sveta Buben]]></dc:creator>
            <pubDate>Tue, 21 Jan 2020 23:41:02 GMT</pubDate>
            <atom:updated>2020-02-25T23:20:26.784Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part 3: pass data dynamically to a generator function</h3><p>This is a continuation of<strong> Understanding Generators</strong> series. The previous article explained how a basic generator function works under the hood.</p><p>We will be looking at this code snippet to understand how to pass data to a generator function.</p><pre>function* createFlow() {</pre><pre>  const num = 10;</pre><pre>  const newNum = yield num;</pre><pre>  yield 5 + newNum;</pre><pre>}</pre><pre>const returnNextElement = createFlow();</pre><pre>const element1 = returnNextElement.next();</pre><pre>const element2 = returnNextElement.next(2);</pre><p>This diagram illustrates what is happening under the hood when JS engine runs the code snippet above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qlDQMCFimj0suHh2ycTzcg.png" /></figure><p>(1) At first, JS declares a variable <em>createFlow</em> and assign to it the whole generator function definition.</p><p>(2) Secondly, it declares a variable <em>returnNextElement</em> which is <em>undefined</em> at the beginning. JS executes a generator function and assigns to <em>returnNextElement</em> variable a returned value from this execution. The invocation of <em>createFlow</em>generator function returns a special generator object. This object has some properties on it, but the one we are interested in here is <em>next</em> property. If we call <em>.next()</em> method for the first time it will create a new Execution Context for <em>createFlow</em>generator function, as <em>next</em> method has a special bond to the <em>createFlow</em> generator function, which allows creating an Execution Context for it. Subsequent calls of <em>next</em> method will re-enter this context till the moment that <em>done </em>(<em>next </em>method returns an object with properties <em>value</em> and <em>done</em>; for brevity, the diagram returns a <em>value</em> only)<em> </em>property is equal to true.</p><p>(3) Afterward, JS declares a new variable <em>element1</em> in global memory which is <em>undefined</em> for now. It goes and executes <em>returnNextElement.next</em> function and assigns to <em>element1</em> a value that is being returned from returnNextElement.next function. When the invocation of returnNextElement starts a new Execution Context of the generator function is being created. (a) Firstly, <em>num</em> variable is being declared in a local function memory. The value assigned to it is 10. (b) The next line that is being run is <em>const</em> <em>newNum = yield num. </em>In the local function memory, JS creates a variable <em>newNum</em> and assign it to <em>undefined</em> for a moment. When JS hits the special word <em>yield</em> it immediately returns <em>num</em> variable value, without managing to assign anything yet to <em>newNum</em> variable. At this moment it is still <em>undefined</em>.</p><p>(4) At last, JS declares a variable <em>element2</em>, initially assigning to it a value of <em>undefined</em>. It will be equal to a returned value of running <em>returnNextElement.next </em>method. This time <em>next</em> method is being called with an argument <em>2</em>. This is a value that will be assigned to <em>newNum</em>, as yie<em>l</em>d caused immediate return from a function leaving <em>newNum</em> undefined. (d) <em>yield 5 +</em> <em>newNum</em> resolves to <em>yield 5 + 2</em>. Therefore, 7 is being returned and assigned to <em>element2</em> variable.</p><p>Did you manage to read through the diagram and explanation attached to it? Please leave your comment 💖</p><p>☞ The following article in this series will explain async generators</p><p>Resources:<br>☛ <a href="https://frontendmasters.com/courses/javascript-new-hard-parts/">https://frontendmasters.com/courses/javascript-new-hard-parts/</a> — by Will Sentance</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=70b860508c99" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Generators in JS. Part 2 (getting next element with a generator function)]]></title>
            <link>https://medium.com/@svetabuben/understanding-generators-in-js-part-2-getting-next-element-with-a-generator-function-51ae3e954cd9?source=rss-24428562d375------2</link>
            <guid isPermaLink="false">https://medium.com/p/51ae3e954cd9</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[iterators]]></category>
            <category><![CDATA[generator]]></category>
            <dc:creator><![CDATA[Sveta Buben]]></dc:creator>
            <pubDate>Tue, 21 Jan 2020 16:29:24 GMT</pubDate>
            <atom:updated>2020-02-25T23:19:16.033Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part 2: getting next element with a generator function</h3><p>This is a continuation of<strong> Understanding Generators</strong> series. A previous example is getting the next element iteratively. It showcases the basic idea of getting the next value, which is what generators do.</p><p>If you’ve heard about generators and never understood how it works there is an amazing explanation on <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*">MDN</a>. I want to know what is happening under the hood when Ideal with generator functions, and I found that diagrams help me to see more clearly what is actually happening.</p><pre>function* createFlow() {</pre><pre>  yield 4;</pre><pre>  yield 5;</pre><pre>}</pre><pre>const returnNextElement = createFlow();</pre><pre>const element1 = returnNextElement.next();</pre><pre>const element2 = returnNextElement.next();</pre><p>This diagram illustrates what is happening under the hood when JS engine runs the code snippet above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vi6Bxl19Bwe-HuAV1EX7YQ.png" /><figcaption>Generator Function — how it works under the hood</figcaption></figure><p>At first <em>createFlow</em> generator function is declared in global memory. It is assigned to a generator function definition.</p><p>Secondly, <em>returnNextElement</em> is declared in global memory and it will be assigned to a returned value of the invocation of <em>createFlow</em> generator function, but at the very beginning, it is <em>undefined</em>. When JS calls <em>createFlow</em> generator function it returns a special object — a generator object. It has some properties in it like a location where the generator function was suspended, but the one that we are interested in is a <em>.next</em> property which is a function that brings us back to a generator function Execution Context.</p><p>Afterward, <em>element1</em> variable is declared in global memory and it will be assigned to a returned value of the invocation of <em>returnNextElement.next() </em>method, but at the very beginning, it is <em>undefined. next </em>method on <em>returnNextElement </em>object<em> </em>has a special bond to the generator function<em>,</em> therefore, when JS calls this method it creates a brand new Execution Context of the generator function <em>createFlow*</em>, which is not being garbage collected after JS finished execution of a function but is being saved across re-enters. You can re-ented Context with .next() method of the generator object.</p><p>The first line JS comes across inside of the generator function is yield 4. <em>yield</em> is a special keyword that specifies the value to be returned immediately from a function. Therefore, we assign value 4 to <em>element1</em> as a returned value of invocation of <em>.next() </em>method on <em>returnNextElement </em>object.</p><p>At the very end, <em>element2</em> variable is declared in global memory and it will be assigned to a returned value of the invocation of <em>returnNextElement.next(). </em>JS does not create a brand new Execution Context, instead, it re-enters the context of the generator function. Generator object holds the location where the execution of generator function was suspended, thus the next line is <em>yield 5</em>. 5 is being returned and assigned to <em>element2.</em></p><p>Was it helpful for you? Please leave a comment 💖</p><p>An upcoming article in this series will describe how to dynamically pass data to a generator function</p><p>Resources:<br>☛ <a href="https://frontendmasters.com/courses/javascript-new-hard-parts/">https://frontendmasters.com/courses/javascript-new-hard-parts/</a> — by Will Sentance</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=51ae3e954cd9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Generators in JS. Part 1.]]></title>
            <link>https://medium.com/@svetabuben/understanding-generators-in-js-part-1-b0fe2d983cf5?source=rss-24428562d375------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0fe2d983cf5</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sveta Buben]]></dc:creator>
            <pubDate>Tue, 14 Jan 2020 21:38:45 GMT</pubDate>
            <atom:updated>2020-02-25T23:17:25.520Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part 1: getting next element iteratively</h3><p>Code snippet provided below mimics the generator behavior by returning the next element in an array using a closure (here Closed Over Variable Environment)</p><pre>function createFlow(array) { <br>  let i = 0;<br>  const inner = {<br>    next: function () {<br>      const element = array[i];<br>      i++;<br>      return element;<br>    }<br>  }<br>  return inner<br>}</pre><pre>const returnNextElement = createFlow([4,5,6]);</pre><pre>const element1 = returnNextElement.next();</pre><pre>const element2 = returnNextElement.next();</pre><p>A picture below illustrates what is happening under the hood when JavaScript runs the code above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_NK6fwWsV-g_8_oHOqXs-Q.png" /></figure><p>Here comes an explanation.</p><p>The right column is a JS global memory. This is a place where JavaScript stores variables and functions. JavaScript is single-threaded, meaning code is being executed line by line. Therefore, everything here is presented in the order it has been executed (omitting hoisting here for simplicity).</p><p>The middle column is a global execution thread. Let’s say we are declaring a variable <em>returnNextElement. </em>JS assigns to it a<em> </em>value that is being returned by invoking a right-hand side function, here<em> createFlow([4,5,6]) (</em>at the very beginning <em>returnNextElement </em>is<em> </em>equal<em> </em>to <em>undefined)</em>. When JS calls a function, it creates a brand new <em>Local Execution Context (Local Variable Environment)</em>. And it is adding at the same time this function to the <em>Call Stack</em> (left column), so JS knows where to look for variables used inside the function. Green arrows entering in and out of Local Execution Context demonstrate the execution thread that is entering the function and leaving it. As soon as JS finished running the code it is being garbage collected and it pops this function of the <em>Call Stack</em>.</p><p><em>returnNextElement</em> is an interesting example as it uses a very powerful JS technique a <em>closure. </em>Therefore<em> array </em>and<em> i </em>variables are still available even after JS finished execution of <em>createFlow([4,5,6])</em></p><p>See, if you can read through this diagram and leave your comment 💫</p><p>Resources:<br>☛ <a href="https://frontendmasters.com/courses/javascript-new-hard-parts/">https://frontendmasters.com/courses/javascript-new-hard-parts/</a> — by Will Sentance</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0fe2d983cf5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript: setTimeout under the hood (technical literacy)]]></title>
            <link>https://medium.com/@svetabuben/javascript-settimeout-under-the-hood-technical-literacy-dc70edbc2698?source=rss-24428562d375------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc70edbc2698</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[settimeout]]></category>
            <dc:creator><![CDATA[Sveta Buben]]></dc:creator>
            <pubDate>Mon, 23 Jul 2018 13:29:58 GMT</pubDate>
            <atom:updated>2019-12-18T12:29:42.957Z</atom:updated>
            <content:encoded><![CDATA[<p>These are my notes taken during the ‘JavaScript Hard Parts` course on FrontendMasters given to us by Will Sentance. I am really glad I came across this course as it enlighten me what is really happening in JavaScript world.</p><p>Let’s take a look at this snippet, and understand how the JavaScript interpreter works.</p><pre>function printHello() {<br>   console.log(‘Hello’);<br>}<br>function blockFor1Sec(){<br>   // blocks in the JavaScript thread for 1 second<br>}<br>setTimeout(printHello, 0);<br>blockFor1Sec();<br>console.log(‘Me first’);</pre><blockquote>Javascript is single threaded, meaning that code is being executed synchroniously line by line.</blockquote><blockquote>As soon as we start running our code we create global execution context.</blockquote><p>So what is happening first ?</p><p>First, we declare in memory named function printHello . Note that under the label printHello we store the whole function definition. Meaning if you would console.log(printHello) the browser will show you:</p><pre>function printHello() {<br>   console.log(‘Hello’);<br>}</pre><p>Then we are declaring (in memory) another named function blockFor1Sec . The function doesn’t have a body in this case. We just want to illustrate here that it will run for 1000ms. Let’s say it has a loop that has some heavy operations in it, which overall will take about 1s.</p><p>You might be wondering why the next line of code is not console.log(‘Hello’)? Only when a function is being called (has parenthesis after it, i.e. myFunction() ) we are getting into a function body.</p><p>Let’s recap and illustrate what we have done already. We have declared in memory two named functions printHello and blockFor1Sec. Note we are in global execution context still.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_AyfGTOaxTzvKMBXnwzM1g.png" /></figure><p>Our next line of code says us to call setTimeout with two parameters printHello and 0 ( the delay time until the function that has been passed as a first parameter will be called). More detailed information about setTimeoutfunction you can find on <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Reasons_for_delays_longer_than_specified">MDN</a>.</p><p>setTimeout is a Timer feature of Web API. It is important to understand that this is not JavaScript function. When we call setTimeout Web API adds on two properties <em>completed</em> and <em>onCompletion </em>( much more operations are happening under the hood, but for clarity, we will generalize it to two properties only)<em>. onCompletion </em>property is a function that was passed to setTimeoutas the first parameter.</p><p>In our case we are delaying for 0 ms, therefore <em>completed</em> property will be true straight away.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y5nZcWyhPHDQzYaG2V91Ig.png" /></figure><p>However, theprintHello function will not be called, but will be pushed to the <strong>callback queue.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IOM7t5RFOTKqdhuBi49VMw.png" /></figure><p>The<strong> event loop</strong> is constantly checking when our printHello function from <strong>callback queue</strong> can be pushed to the <strong>call stack</strong>.</p><p>[!] Once there is no synchronous code left in the global context, printHello function will be pushed to the call stack. But it is not the moment yet, because we still have some functions to invoke. We are left off with:</p><pre>...<br>blockFor1Sec();<br>console.log(‘Me first’);</pre><p>Next blockFor1Secis being invoked. It creates its own execution context as well as its being pushed to the call stack, so JavaScript engine knows what execution context we are currently in. The function runs for 1000 ms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SatAr2V9dq-koQNJmxMTSg.png" /></figure><p>Once blockFor1Secfinished execution it is being popped off the call stack and we are back into the global context. Meanwhile, garbage collector destroyed execution context of blockFor1Sec.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bscvSJyThCvG_Vf3qUyr3g.png" /></figure><p>Next, ‘Me first’ is logged into a console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b1d5McDxDRUkiMQPf-Aelw.png" /></figure><p>Now when we have completed everything in the global execution context, it is time to push printHello to the call stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*biNQJSZFvdbbQEFy997-Eg.png" /></figure><p>Calling printHello creates new execution context. Once it is done we are poping it off the call stack and garbage collector destroys its execution context.</p><p>Now we know how setTimeout function of Web API works. It is time to practise what we have learned 👩🏼‍💻</p><p>JavaScript interview question from <a href="https://www.toptal.com/javascript/interview-questions">Toptal</a></p><ol><li>What will be the output of the following code (speak through you solution).</li></ol><pre>for (var i = 0; i &lt; 5; i++) {<br>	setTimeout(function() { console.log(i); }, i * 1000 );<br>}</pre><p>2. In what order will the numbers 1–4 be logged to the console when the code below is executed? Why?</p><pre>(function() {<br>    console.log(1); <br>    setTimeout(function(){console.log(2)}, 1000); <br>    setTimeout(function(){console.log(3)}, 0); <br>    console.log(4);<br>})();</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc70edbc2698" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>