title: Rewrite Javascript Function using Promises
body: <p>I'm stuck with updating my code to use promises now.</p>
  
  <p>The hash function is working, but changed. It now returns a promise and not a value anymore. And I don't know how to use it :-(</p>
  
  <pre><code>function hash(input) {
      // transform the input into an arraybuffer.
      let buffer = new TextEncoder("utf-8").encode( JSON.stringify(input) );
  
      return window.crypto.subtle.digest("SHA-256", buffer).then( (hash)=&gt; {
          return btoa(String.fromCharCode( ...new Uint8Array(hash) ));
      });
  }
  </code></pre>
  
  <p>This is the function that needs to be rewritten. hash(...) is called several times.</p>
  
  <pre><code>function update(){
  
      // array of Objects (empty to simplify my question)
      let data = [{}, {}, {}];
  
  
      // if data has changed =&gt; process it
      if( hash(data) !== hash(previousData) ){
  
          // clone for next time comparison (before calculating hashes!)
          previousData = JSON.parse(JSON.stringify(data)); // simple deepclone 
  
          data.forEach( item=&gt;{
              // reset dynamic values within a cloned item for simple item comparison checks  
              item.id = hash( Object.assign( {}, item, {dynValue:1}) ); 
  
              //complet item hash including dynamic values
              item.hash = hash( item );
          });
  
          // fire event with changed data array
          // ...
      }
  }
  </code></pre>
  
  <p>I'm basically creating two hashes for each object within the array to simplify later comparison. One with some ignored properties (reset to default values) and one for the complete object.</p>
  
  <p>Thank you for helping me out </p>