Only trigger a function in the last iteration of a forEach loop
<p>The following code checks if a field is of type file and if there's an actual file in it. If that's the case, upload the photo and update the buildings. If that's not the case, update the building using the old photos: </p>

<pre><code>  fields.forEach(field =&gt; {
    building[field.name] = field.value || this.building[field.name]
    if (field.type === 'file' &amp;&amp; !util.isEmpty(field.photo.file)) {
      api.uploadPhoto(field.photo.file).then(resp =&gt; {
        building[field.name] = resp
        this.updateBuilding(building)
      })
    } else {
      building.logo = this.building.logo // to prevent updating logo
      building.floorplan = this.building.floorplan // to prevent updating logo
      this.updateBuilding(building)
    }
  })
</code></pre>

<p>It works well, but since <code>this.updateBuilding(building)</code> is in a loop, it's being called multiples.</p>

<p>How to do it so it's only called in the last iteration?</p>