[Ndn-interest] Managing ever-growing callback chains

Nick Gordon nmgordon at memphis.edu
Thu Apr 20 16:34:07 PDT 2017

At the risk of being given a link to tutorialspoint, I thought I'd get some input from people who have either contrived their own solutions
to this problem or have made peace with it and have some wisdom to share.

I've been noticing this trend for a while now, that as the number of async operations in ndn-cxx grows, the necessary number of callback
parts you need to split some "logical" function into grows as well. That is, what used to be a single, long function becomes many smaller
functions whose code is split along where you're making the async calls.

Until now I've been dutifully splitting my code into smaller bits where needed, usually using lambdas. However, lambdas in C++ are not
exactly ideal. Consider the case where you have both a success and a failure callback; if you use descending lambda chains (in the sort of
node.js style) to handle this, it will very quickly turn into an unreadable, confusing mess of indents and braces.

Another option is to simply split each portion of your original, "logical" function into separate named functions, and call the next one in
the sequence when necessary. This sort of violates the principle that a function should be reusable in some way, as I think many of these
named functions end up being highly context-dependent. I also feel that even with a moderate amount of async calls, this leads quickly to a
spaghetti-style mess of functions where you cannot tell which functions go with which, and simply figuring out how something works requires
drawing graphs and/or complex tools that connect the dots for you.

Something that C++ provides is async/future mechanisms, but I've been told that we can't use multi-threading, and since futures/promises
execute in multiple threads, we can't do that due to some limitations I'm not aware of.

I know this is somewhat ranty, but the short version: How do you other developers deal with lots of async calls in your C++ code? There
seems to be no "good" way of doing it.


More information about the Ndn-interest mailing list