简体   繁体   中英

javascript promise rejects in two places

I work with promise-based libraries such as moment.js and axios . This time I want to create a promise by myself.

I have a function to send emails. Simply what I want is if there is no error resolve the promise and if there is an error reject the promise.

 export default async function password_reset_request(email, user_name, link) { return new Promise((resolve, reject) => { try { var transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: 'something@gmail.com', pass: 'secret' } }); transporter.sendMail(mailOptions, function (error, info) { if (error) { reject() } else { resolve() } }); } catch (error) { reject() } }) } 

Is this technique correct? I am rejected for my promise in two places.

 export default async function password_reset_request(email, user_name, link) { return new Promise((resolve, reject) => { try { var transporter = await nodemailer.createTransport({ service: 'gmail', auth: { user: 'something@gmail.com', pass: 'secret' } }); transporter.sendMail(mailOptions, function (error, info) { if (error) { reject(); } else { resolve(); } }); } catch (error) { reject(); } }); } 

Please check this correction.

A few issues:

  • You don't need an async function when you don't use await . Your function returns a promise already, so there is no need for async .
  • There is no need to have a try...catch block inside a promise constructor callback function: if an exception occurs there it will automatically be converted to a rejection with the error as reason. NB: The same principle holds in a then callback.

The promisifying you did for sendMail is correct. It would be best practice to create a dedicated function for that alone (so without createTransport ). It would also be good to pass useful information to reject and resolve :

function sendMailPromise(transporter, mailOptions) {
    return new Promise((resolve, reject) => {
        transporter.sendMail(mailOptions, function (error, info) {
            return error ? reject(error) : resolve(info);
        });
    })
}

export default function password_reset_request(email, user_name, link) {
    var transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'something@gmail.com',
            pass: 'secret'
        }
    });
    return sendMailPromise(transporter, mailOptions);
}

There are some unused variables ( email , user_name , link ) and variables which seem to be global ( mailOptions ): you would need to improve that situation.

Can we use resolve or reject function in more then one place?

Yes, we can there's no issue. It's similar to return different things under different conditions.

Have you used promises correctly?

No. Why you are using async function and promise at same time? What's use of creating function async?

Just modify code to this.

export default function password_reset_request(email, user_name, link) {
    return new Promise((resolve, reject) => {
        var transporter = nodemailer.createTransport({
            service: 'gmail',
            auth: {
                user: 'something@gmail.com',
                pass: 'secret'
            }
        });

        transporter.sendMail(mailOptions, function (error, info) {
            if (error) {
                reject()
            } else {
                resolve()
            }
        });
    })
}

Will suggest you to read docs for any feature of JavaScript you want to use. JavaScript provide multiple functions for same functionality, we need to decide which works best for our case, in place of using all at once.

Do go through these docs async-await , promise in Javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM