1

I'm using AWS to upload files to an S3 bucket. In the below code snippet, I'd like to update the value of uploadId but the the value returns undefined. I know its because the last line completes before the s3... block. How do I kind of wait for the s3 block to complete before returning the updated value of uploadId?

let uploadId
let params = { Bucket: s3_bucket, Key: filename, ContentType: filetype }
s3.createMultipartUpload(params, (err, data) => {
    uploadId = data.uploadId
    console.log(uploadId)
    console.log("uploadId err: ", err)
})
return uploadId
chidimo
  • 2,684
  • 3
  • 32
  • 47
  • Return a promise intead. [Here](https://medium.com/@samthor/js-callbacks-to-promises-541adc46c07c) is an example. – HMR Feb 18 '19 at 11:04
  • Are you sure your S3 settings are correct ? I'm not sure the problem is about async. Could you print the `err` variable ? – samb102 Feb 18 '19 at 11:09
  • There's no error. When I log the uploadId I get the correct string. But that return statement is reached before s3 has had time to finish its operation. I'm trying to make it into a promise but so far the syntax still confuses me. I see `setTimeOut` in a lot of examples and I'm not sure what its role is. – chidimo Feb 18 '19 at 11:11
  • `await s3.createMultipartUpload(params)` also throws an error `await is a reserved word`. – chidimo Feb 18 '19 at 11:14

0 Answers0