Monday, 15 May 2017

Pass additional variable to callback without changing the callback signature

I'm using our company's AWS S3 API wrapper. To download files from cloud I call the following wrapper:

aws.s3.downloadFile(bucket, fileName, cbDownloadOk, cbDownloadErr);

Inside of this function we build parameters' container and then call the official AWS S3 API-function:

s3.listObjectsV2(params, function(err, data) {
    if (err) {
        cbDownloadErr(err);         // an error occurred
    } else {
        cbDownloadOk(data);         // successful response
    }
});

Now I want to be able to print the name of downloaded file, e.g.:

var cbDownloadOk = function (data) {
    console.log("File " + fileName + " downloaded");
}

The problem is that I can't change the implementation of wrapper and hence to change the signature of the cbDownloadOk callback.

My question:
Is there any way to pass to cbDownloadOk the fileName without changing the implementation of the wrapper? Is usage of global variable is the only way to do that?



via Mike B.

No comments:

Post a Comment