Asynchronous programming -promise Chain Meditations

A, promise composition

1, task: promise to complete the task;

2, result: processed data;

3, status: status;

4, fulfill, reject (corresponding to the catch)

5, ResolveCallback ErrorCallback promise status interpretation function    

6、resolve:

To explain the current state of promise, has completed the implementation of state immediately back off, unfinished state registration back off function;

7, then: a promise back out before registration, after leading a promise;

 

Two, promose state machine:

fulfill, reject: state change function, input; preamble; may be performed by the internal and external functions;

Surface interface is a task; multiple passes through the interior of the task may be;

ResolveCallback ErrorCallback: changing the output state of the function; follow;

 

Three, promise of the life cycle (for example asynchronously)

1, promiseA create, execute asynchronous task functions;

2, then calls the create promise relay node (promiseZ), then while the package (promiseX) configured and promiseZ input (fulfill) (promiseA.resolve ()) is transmitted to promiseA.resolveCallbacks [];

3, promiseA task, run promiseA.fulfill, to fulfill commitments if promiseA;

4, promiseA.fulfill successor function call outwardly transmission state;

5, resolveCallback performed, promiseX first generated, and then registered as the subsequent promiseZ fulfill the function of promiseX;

6, promiseX task is completed, the execution fulfill promiseX, resolveCallback calling fulfill promiseZ of;

7, promiseZ the state of completion of commitments; call subsequent promise modules;

 

Fourth, functional programming environment variables - analog station recursive function calls

Construction of an asynchronous procedure call chain promise building process corresponding to the recursive call;

It corresponds to execution of the reverse strand promise execution recursive call;

 

The key is to clarify the configuration and implementation of a function to figure out every block or environment variables;

In particular block, its environment variables may span several modules promise;

 

block of environment variables:

1, promise self function represented;

2, the function of the input parameters;

3, block-generated environment variables

 

@discardableResult func then<U>(_ next: @escaping ((T) -> Promise<U>) ) -> Promise<U> {

        return Promise<U> { fulfill, reject in

            self.catch { (err) in

                reject(err)

            }.resolve { result in

                next(result).catch { (err) in

                    reject(err)

                }.then { (finalResult) in

                    fulfill(finalResult)

                }

            }

        }

    }

 

Ordinary function call to save the environment variable call stack by global functions and functions;

block by block to preserve the environment variable objects;

 

Fourth, the asynchronous programming model

 

Five, promoie (task) function

promiseA: asynchronous start function function, after the completion of the asynchronous fulfill promise, call the successor function;

promiseX: then fulfill the functions of input, and as the successor of promiseA promiseZ preamble;

Generated by the promise constructor (transform monand in the flatmap);

promiseZ: promiseX responsible for constructors and fulfill their packaging, to pass through promiseA resolve promiseA; the same relay behind Promise;

 

promiseZ as promise.then environment variables;

 

 

Six, promise constructor, then, back off function pipelined

@discardableResult func then<U>(_ next: @escaping ((T) -> Promise<U>) ) -> Promise<U> {

        return Promise<U> { fulfill, reject in

            self.catch { (err) in

                reject(err)

            }.resolve { result in

                next(result).catch { (err) in

                    reject(err)

                }.then { (finalResult) in

                    fulfill(finalResult)

                }

            }

        }

    }

 

七、promise、flatMap

trait Monad[M[_]] {

 def unit[A](a: A): M[A]

 def flatMap[A, B](fa: M[A])(f: A => M[B]): M[B]

The promise to build the object as a process,

Only transform: @escaping ((T) -> Promise <U>) execution is completed, promiseZ considered been constructed;

 

Guess you like

Origin www.cnblogs.com/feng9exe/p/11113253.html