context: Two javascript classes in separate files, each integrating a different external service and being called in a express.js router.
See "problematic code" below:
routes.post('/aws', upload.single('file'), async (req, res) => {
const transcribeParams = JSON.parse(req.body.options)
const bucket = 'bucket-name'
const data = await ( await ( await awsTranscribe.Upload(req.file, bucket)).CreateJob(transcribeParams)).GetJob()
res.send(data)
})
class AmazonS3 {
constructor() {
this.Upload = this.Upload
}
async Upload(file, bucket) {
const uploadParams = {
Bucket: bucket,
Body: fs.createReadStream(file.path),
Key: file.filename,
}
this.data = await s3.upload(uploadParams).promise()
return this
}
}
class Transcribe extends AwsS3 { constructor() { super() this.CreateJob = this.CreateJob this.GetJob = this.GetJob } async CreateJob(params) { if(this.data?.Location) { params.Media = { ...params.Media, MediaFileUri: this.data.Location } } this.data = await transcribeService.startTranscriptionJob(params).promise() return this } async GetJob(jobName) { if(this.data?.TranscriptionJob?.TranscriptionJobName) { jobName = this.data.TranscriptionJob.TranscriptionJobName } this.data = await transcribeService.getTranscriptionJob({TranscriptionJobName: jobName}).promise() return this } }
problem: the problem is with the chained awaits in the router file:
await ( await ( await awsTranscribe.Upload...
Yes, it does work, but it would be horrible for another person to maintain this code in the future.
How can i make so it would be just
awsTranscribe.Upload(req.file, bucket).CreateJob(transcribeParams).GetJob() without the .then?
PS: For those wondering why the last code is in quotes, it was accusing it of not properly formatted code.