Change your code to this (assuming that your top-level function can await
):
const msg_bo = new msgBO();
const msg = JSON.parse( req.body.message );
await uploadFile();
function upload(){
return new Promise( ( resolve, reject ) => {
const cnf = msg_bo.uploadMessageAttachments( msg, req.file );
resolve( cnf );
} );
}
async function uploadFile() {
const cnf = await upload();
console.log( 'my response ', cnf );
res.send( {
status: 'success',
message: {
text: msg,
filename: cnf
}
} );
}
The above can be simplified to this:
const msg_bo = new msgBO();
const msg = JSON.parse( req.body.message );
const cnf = await upload();
console.log( 'my response ', cnf );
res.send( {
status: 'success',
message: {
text: msg,
filename: cnf
}
} );
function upload(){
return new Promise( ( resolve, reject ) => {
const cnf = msg_bo.uploadMessageAttachments( msg, req.file );
resolve( cnf );
} );
}
...though as you're not actually using any real async
API, you can do it all synchronously (assuming that uploadMessageAttachments
doesn't return a Promise<T>
):
const msg_bo = new msgBO();
const msg = JSON.parse( req.body.message );
const cnf = msg_bo.uploadMessageAttachments( msg, req.file );
console.log( 'my response ', cnf );
res.send( {
status: 'success',
message: {
text: msg,
filename: cnf
}
} );