0

Code below is returning a Promise Object in form of Promise{object}. I am unable to get it in actual object

var msg_bo = new msgBO();
   var msg = JSON.parse(req.body.message);
   var cnf; 


   uploadFile()

   function upload(){
      return new Promise((resolve,reject)=>{
         cnf = msg_bo.uploadMessageAttachments(msg, req.file);
         resolve()
      })
   }

   async function uploadFile(){
         await upload()
         console.log('my response ',cnf)
         res.send({status:'success',message: {
            text:msg,filename:cnf
         }})

   }
Amaan Imtiyaz
  • 254
  • 6
  • 16
  • 1
    You need to pass an argument to `resolve()`, I assume `resolve( cnf )`. And `cnf` should not be declared in the root function because that makes the closure-capture more difficult to reason about. – Dai Feb 11 '20 at 04:52
  • @Dai it didn't work – Amaan Imtiyaz Feb 11 '20 at 05:13
  • What does `uploadMessageAttachments` do, does it have an asynchronous callback? If it's not async, then you shouldn't use a promise here at all, if it is, you shouldn't `resolve()` immediately – Bergi Feb 11 '20 at 10:37

2 Answers2

0

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
    }
} );
Dai
  • 141,631
  • 28
  • 261
  • 374
0

this should work,

   var msg_bo = new msgBO();
   var msg = JSON.parse(req.body.message);
   var cnf; 


   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
         }})

   }
laxman
  • 1,781
  • 4
  • 14
  • 32