Chaining functions with callbacks while keeping se

2019-05-14 07:37发布

In the codebase I picked up, we have a chain of async methods that expands to two modules with different functional purposes.

i.e we do some stuff with GeneralTravel and then pass on the data to Hotel related stuff:

Travel.processRequest(req){
   //... then
   processHotel(req, Hotel.makeCall);
}

Travel.processHotel(.., callback){
   //...
   callback(null, Hotel.fillDB); // 1  calls MakeCall
}

Hotel.makeCall(err, callback){
   //...
   callback(); // 2
}

Hotel.fillDB(){

   //...
   return;
}

My problem with this code flow is (apart from the fact that it's a torture to follow),

  1. I donT want to decide from within Travel, what Hotel should do with its data
  2. Hotel.makeCalldoesn't even know what its callback is! //see 1 & 2 in code

While in other cases this code design might fit well, i think we need an improvement here.

Is it the organization of modules? naming? the call chain?

PS: in a world without async! how was life? :)

0条回答
登录 后发表回答