<?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: Vlad</title>
    <description>The latest articles on DEV Community by Vlad (@alittlebyte).</description>
    <link>https://dev.to/alittlebyte</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%2F280595%2Fbb9842e4-4323-48d8-845a-3e189229c0d1.png</url>
      <title>DEV Community: Vlad</title>
      <link>https://dev.to/alittlebyte</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alittlebyte"/>
    <language>en</language>
    <item>
      <title>Using a Promise in a click eventListener - more than once.</title>
      <dc:creator>Vlad</dc:creator>
      <pubDate>Sun, 26 Jan 2020 17:41:37 +0000</pubDate>
      <link>https://dev.to/alittlebyte/using-a-promise-in-a-click-eventlistener-more-than-once-43hf</link>
      <guid>https://dev.to/alittlebyte/using-a-promise-in-a-click-eventlistener-more-than-once-43hf</guid>
      <description>&lt;p&gt;Hello everyone,&lt;br&gt;
I'm trying to wrap my head around how the vanilla MVC should be done right, and existing resourses have been of great help. Now I need help with something I can't seem to find - or formulate a proper query for.&lt;br&gt;
Here's the relevant part of the code that I have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        //view
        class CollectorView{
            constructor(){
                this.element1 = document.getElementById('collections');
                this.element2 = document.getElementById('collections__photos');         
            }

            ...

            returnCollectionsId(e){
                return new Promise((resolve) =&amp;gt; 
                    this.element1.addEventListener('click', e =&amp;gt; 
                        (!e.target.id || e.target.id === 'collections') ? null : resolve(e.target.id)                   
                    )
                )
            } 

            async renderPhotos(viewModel){
                this.element2.innerHTML = viewModel.map(el =&amp;gt; `&amp;lt;div&amp;gt;
                    &amp;lt;h3&amp;gt;${el.description?el.description:el.alt_description}&amp;lt;/h3&amp;gt;
                    &amp;lt;a href="#photos__details" class="circle" id="${viewModel.indexOf(el)}"&amp;gt;&amp;lt;/a&amp;gt;
                &amp;lt;/div&amp;gt;`)
                .join("");
            }
        }

        //controller
        class CollectorController{
            constructor(model,view){
                this.model = model
                this.view = view

                this.getId()
            }

            ...

            async getId(){
                let photosId = await this.view.returnCollectionsId()
                let photosPromise = this.model.sendPhotos(photosId)
                let photosArray = await photosPromise.then(rsp =&amp;gt; rsp.data)
                this.view.renderPhotos(photosArray)
            }
        }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So what happens is: there's a click eventListener that, when a certain element is clicked, reads the id of the clicked element, sends it into the model and does an API call with it, receives the info and renders it.&lt;/p&gt;

&lt;p&gt;While this works perfectly, there are two big problems:&lt;br&gt;
1) Promise only runs once. When it resolves with an existing ID, I can't go to the previous view and click a different element - it would have already run and still renders the first thing clicked.&lt;br&gt;
2) The await part of the async/await only will work as intended when it receives a Promise. So can't just pass an ID as a string/number - returns undefined and everything breaks.&lt;/p&gt;

&lt;p&gt;So, is there an alternative that would also return a promise, but would work anew every time upon a click happening? Or something to replace async/await with? &lt;/p&gt;

&lt;p&gt;Thank you in advance! &lt;/p&gt;

</description>
      <category>help</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
