Saturday, 29 April 2017

how to bypass multiple then in of mongoose

I Don't want to make checks as if (!isPresent) then do this ....

Is their any way to bypass all the then when my work is complete

var isPresent=false;
    myPromise
            .then((employee) => {
                    if (employee) {
                        // throw 'employee already exist';
                        isPresent = true;
                        return res.data(mapper.toFullModel(employee));

                    }
                return model;
            });
            .then(model => {
                if (!isPresent) {
                    return new db.employee(model).save();
                }
            })
            .then(employee => {
                if (!isPresent) {
                    employee.token = auth.getToken(employee);
                    return employee.save();
                }
            })
            .then(employee => {
                if (!isPresent) {
                    res.data(mapper.toFullModel(employee));
                }
            })
            .catch(err => {
                res.failure(err);
            });



via hardy

No comments:

Post a Comment