jeffrey chan
jeffrey chan

Reputation: 175

how to void callback hell in typescript

I know callback hell is bad, but I can't solve this problem in typescript. if I want to remove a category:

first check the category exists second check the category has parent?

if has update category's parent children collection

finally callback to controller send message to client.

anybody could help me out? here is my method code:

removeCategory(catId: string, callback: (error: any, result: any) => void) {
    var msg = {
        "message": "",
        "statescode": 200,
        "error": null
    };
    console.log("find category");
    if (catId) {
        super.findById(catId, (getError: any, getEntity: any) => {
            if (getError) {
                msg.message = "can't find this item: " + catId;
                msg.statescode = 404;
                msg.error = getError;
                callback(msg, null);
            } else {
                console.log("find category");
                super.remove(catId, (delError: any, delEntity: any) => {
                    if (delError) {
                        msg.message = "something wrong with remove this category: " + getEntity.name;
                        msg.statescode = 400;
                        msg.error = delError;
                        callback(msg, null);
                    }
                    else {
                        if (getEntity.parent) {
                            var parentId = getEntity.parent;

                            super.update(parentId, { $pull: { childrens: getEntity._id } }, (putError: any, putEntity: any) => {
                                if (putError) {
                                    msg.message = "can't update this item:" + item.name;
                                    msg.statescode = 500;
                                    msg.error = putError;
                                    callback(msg, null);
                                }
                                else {
                                    callback(null, getEntity);
                                }
                            });
                        } else {
                            callback(null, getEntity);
                        }
                    }
                });
            }
        });
    }
    else {
        msg.message = "entityId can't be empty!";
        msg.statescode = 400;
        callback(msg, null)
    }
}

Upvotes: 2

Views: 1121

Answers (2)

Madhu Ranjan
Madhu Ranjan

Reputation: 17934

There is a proposal in Typescript 2.0 regarding Async Functions which may help you, you may keep eye on that.

You may also look into Async.js library for now, it also helps in this scenario.

Upvotes: 1

jeffrey chan
jeffrey chan

Reputation: 175

using promise in mongoose could fix this problem. here is code:

createCategory(catEntity: ICategoryModel, callback: (error: any, result: any) => void) {
    var parentCat: ICategoryModel;
    var postEntity: ICategoryModel;
    if (catEntity && catEntity.parent) {
        this._categoryRep.findById(catEntity.parent).then((getCat: ICategoryModel) => {
            if (!getCat) {
                throw new Error("can not find this category's parent");
            }
            return getCat;
        }).then((getCat: ICategoryModel) => {
            parentCat = getCat;
            return this._categoryRep.create(catEntity);
        }).then((postCat: ICategoryModel) => {
            postEntity = postCat;
            return this._categoryRep.update(parentCat._id, { $push: { childrens: postEntity._id } });
        }).then((putInfo: any) => {
            callback(null, postEntity);
        }).then(null, (error: any) => {
            callback(error, null);
        });
    } else {
        this._categoryRep.create(catEntity).then((postCat: ICategoryModel) => {
            callback(null, postEntity);
        }).then(null, (error: any) => {
            callback(error, null);
        });
    }
}

Upvotes: 1

Related Questions