Implementing callbacks in javascript
<p>I have a function in my helper js file to upload files on S3:</p>

<pre><code>module.exports.uploadToAWS = function uploadToAWS(folderName, fileName, fileData) {

  var s3 = new AWS.S3({ params: {Bucket: ‘myBucket’} });

  var keyName = folderName + "/" + fileName;

  var buffer = new Buffer(fileData.replace(/^data:image\/\w+;base64,/, ""),'base64')

  var data = {
    Key: keyName, 
    Body: buffer,
    ContentEncoding: 'base64',
    ContentType: 'image/jpeg'
  };

  s3.putObject(data, function(err, data) {
    if (err) { 
      console.log(err);
      console.log('Error uploading data: ', data); 
    } else {
      console.log('succesfully uploaded the image!');
    }
  });
}
</code></pre>

<p>My consumer function is like this:</p>

<pre><code>if(images) {
  images.forEach(function(imageRecord) {
    awsHelper.uploadToAWS('assets', assetObject.id, imageRecord);
  });            
}
</code></pre>

<p>I want to introduce a callback method here, such that I might get the success and failure from my helper function to my caller function. How could it be implemented?
    Need my consumer function to be like the following, but what would be the helper function be like?</p>

<pre><code>if(images) {
  images.forEach(function(imageRecord) {
    awsHelper.uploadToAWS(
      'assets',
      assetObject.id,
      imageRecord,
      function (success, failure) {
        //handle success or failure
      });
  });
}
</code></pre>