0

I am converting the below function

 // send an email
  MyModel.sendEmail = function(cb) {
    MyModel.app.models.Email.send({
      to: 'foo@bar.com',
      from: 'you@gmail.com',
      subject: 'my subject',
      text: 'my text',
      html: 'my <em>html</em>'
    }, function(err, mail) {
      console.log('email sent!');
      cb(err);
    });
  }

into Promise like this

return new Promise(function (resolve, reject) {
            return app.models.AppEmail.send({
              to: message.to,
              from: message.from,
              subject: 'My RM Text',
              text: message.text,
            },
              (err, response) => {
                if (err) {
                  console.log('errr');
                  return reject(err);
                } else {
                  console.log('email sent');
                  return resolve(response);
                }
              });
          });

but I want to completely convert the function into Promise chaining so I am confused how can I convert this part

(err, response) => {
                if (err) {
                  console.log('errr');
                  return reject(err);
                } else {
                  console.log('email sent');
                  return resolve(response);
                }
              });

into .then() block . I am somewhat confused since .then() I think takes only 1 argument and then in catch block if I handle error then how will I call reject

Vikas
  • 975
  • 1
  • 10
  • 34
  • What isn't working? Approach shown appears to be just fine – charlietfl Aug 30 '18 at 11:44
  • Everything is working but I want to remove that callback function also in .then block – Vikas Aug 30 '18 at 11:46
  • The whole point of promisifying that `send` API into a function that returns a promise is that your `send` function *takes a callback and does not yet allow `then` to be used*. – Bergi Aug 30 '18 at 13:28

2 Answers2

0

You can convert callbacks to promises with util.promisify automatically:

const { promisify } = require('util');
const sendEmails = promisify(MyModel.app.models.Emails.send);
sendEmails({ /* hard coded values you used */).then(() => {
  // done
});

See How do I convert an existing callback API to promises?

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • I know this is the new way of converting callbacks into Promise but can you please tell me how to do it by creating new Promise – Vikas Aug 30 '18 at 12:22
0

The Promise code that you used you can make a function and use .then after the function call and can continue you promise chain. change the MyModel.sendEmail function to

 MyModel.sendEmail = function() {
   return new Promise(function(resolve,reject){
     MyModel.app.models.Email.send({
       to: 'foo@bar.com',
       from: 'you@gmail.com',
       subject: 'my subject',
       text: 'my text',
       html: 'my <em>html</em>'
     }, function(err, mail) {
       if (err) {
              console.log('errr');
               reject(err);
            } else {
              console.log('email sent');
               resolve(response);
            }
       });
     })
 }

now you can use this function in Promise chain, example

function1()
.then(() => {
  statement1;
  return MyModel.sendEmail();
})
.then(()=> {
        otherstatements;
})
.catch((err) => {
     console.log(err);
})
Yash Thakor
  • 129
  • 3
  • 10