In Promisekit, there are a lot of recursive operations involved, and recursive operations, for most people are more difficult to understand (including me), and GitHub some of the high-level third-party libraries, no one does not involve a very beautiful recursive operation, so understand recursive operation is necessary. If you want to be the best possible program ape.
Recursive operation: Call themselves, keep the stack, and finally out of the stack. (easy to say, but still more complex to understand) Let's take a look at the recursive operation of Promisekit and see how its chain-like structure is implemented. It is believed in the previous chapter that the then operation returns a new pmkpromise, and then the returned Pmkpromise instance can call the then method again, which is a chain structure (for example). Very simple. If only this recursion, then there is no need to write this article. To take a closer look at its internal recursive operation, to see how it ended up doing so much then to invoke execution, (it was not immediately called then to execute the block's function body, but in a specific case after the execution, such as Uialertview is executed in the case of clicking the button), and see how its value is passed. No more nonsense. Post code: Inside the Resolved:pending function
Dispatch_barrier_sync (_promisequeue, ^{ if (result = _result) return; __block Pmkpromisefulfiller Resolver; Next = [Pmkpromise new:^ (Pmkpromisefulfiller fulfill, Pmkpromiserejecter reject) { resolver = ^ (id o) { if (Iserro R (O)) reject (O); else fulfill (o); }; }]; [_handlers addobject:^ (id value) { Mkpendingcallback (value, Next, Q, block, resolver); }]; });
and a pmkrsolve function
static void Pmkresolve (pmkpromise *this, id result) {void (^set) (id) = ^ (ID r) { Nsarray *handlers = Pmksetresult (this, r); for (void (^handler) (ID) in handlers) handler (R); }; if (ispromise (Result)) {pmkpromise *next = result; Dispatch_barrier_sync (next->_promisequeue, ^{id nextresult = next->_result; if (NextResult = = nil) {//IE. pending [next->_handlers addobject:^ (id o) {pmkresol ve (this, O); }]; } else set (NextResult); }); } else set (result);}
As you can see, the block block of the set is performed by traversing all block blocks of handler that are stored in the _handlers, setting the result value of the pmkresolved, and then emptying all the stored _handler block blocks. This is a block of the subject that will allow our then to block reasonable execution, which has been said in the previous section. Here is a recursive operation of the main body, the following to see how this recursive operation: 1. Determine if result is PMKResolved2. If it is not pmkresolved, the block block of the set is called (the parameter is result) 3. If it is pmkresolved, determine the nextresult of result and determine if it is PMKResolved4. If it's not Pmkreso, Lved, the block block of the set is called (the parameter is NextResult) 5. If it is pmkresolved, then go back to 1 This is the entire recursive process, the operation to open the entire wireless loop is 2 and 4. If it is a Uialertview instance, it will call the 4 operation directly. This is the simplest way to execute the process. Let's just think, if there's a couple of then in the back, then how does this happen, and each pmkresolved will execute his handler, so that each then block is executed,
Dispatch_barrier_sync This function ensures that it is executed sequentially. So the order in which it executes is similar to our line of code executing a block in the then. As for the value of the transfer, we also mentioned above. There's no emphasis here.
The final recursive comparison is difficult to understand, but seriously to read the words can still be, in foreign countries, many colleges and universities are in Lisp or Haskell such a functional language to touch programming, and some have scheme this language curriculum, the main idea of these languages is recursion. So the recursive return is in their deep mind. In many cases, recursion is efficient and the code is concise. But for us it is better to use less in unfamiliar situations. I also try to learn a language and brainstorm.
Promisekit Parsing (ii) recursive IOS