2016-05-20 7 views
2

Ich weiß, Callback-Hölle ist schlecht, aber ich kann dieses Problem in Typoskript nicht lösen. wenn ich eine kategorie entfernen möchte:wie Callbackhölle in Maschinenschrift zu löschen

erst überprüfen sie die kategorie existiert zweitens überprüfen sie die kategorie hat eltern?

wenn Update Kategorie der Eltern Kinderkollektion hat

schließlich Rückrufnachricht an den Client an die Steuerung senden.

kann mir jemand helfen? hier ist meine Methode 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) 
    } 
} 

Antwort

1

Es gibt einen Vorschlag in Typoskript 2.0 in Bezug auf Async Functions ist, die Ihnen helfen können, können Sie im Auge, dass halten.

Sie können auch in Async.js library für jetzt schauen, es hilft auch in diesem Szenario.

Hoffe das hilft !!

+0

Dank für Ihre Hilfe, ich denke, das wird mir helfen: http://www.summa.com/blog/avoiding-callback-hell-while-using-mongoose –

1

mit Versprechen in Mungo könnte dieses Problem beheben. hier ist der 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); 
     }); 
    } 
} 
Verwandte Themen