title: Wait until image is loaded on FS (NPM that stands for File-System)
body: <p>I'm trying to code it so I can pick the main color from an image, I have the picking part done with an NPM, but now I need to get the image itself downloaded. Here's my code I'm using:</p>
  <pre class="lang-js prettyprint-override"><code>if (!message.author.bot) {
      var download = function(uri, filename, callback){
        request.head(uri, function(err, res, body){
          console.log('content-type:', res.headers['content-type']);
          console.log('content-length:', res.headers['content-length']);
  
          request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
        });
      };
      if (message.channel.id = &quot;825209044313571388&quot;) {
        message.delete()
          var channel = client.channels.cache.get('222197033908436994');
            const webhooks = await message.channel.fetchWebhooks();
            const webhook = webhooks.first();
            await download(`${message.author.displayAvatarURL()}`, 'image.webp', function(){
              console.log('done');
            });
            var colorThief = new ColorThief();
            var color = colorThief.getColor(&quot;./image.webp&quot;);
            console.log(color)
            var embed = new MessageEmbed()
              .setColor(color)
              .setDescription(messag.content)
              .setTitle(message.author.username)
              .setThumbnail(message.author.displayAvatarURL())
            await webhook.send(``, {
              username: `${message.author.username}`,
              avatarURL: `${message.author.displayAvatarURL()}`,
              embeds: [embed],
            })
      }
    }
  </code></pre>
  <p>It works, but the code after the &quot;download&quot; function runs without waiting for the image to be downloaded to the system, so it outputs an error.</p>
  <pre><code>UnhandledPromiseRejectionWarning: Error: Image given has not completed loading
  </code></pre>
  <p><strong>TL;DR:</strong> I need help making my code after the function to wait for FS to finish downloading an image.</p>