Thunk
Thunk to call by name, that argument into a temporary function, the function passed to the function and then this temporary body.
function (m){ |
x is a parameter, which put a temporary function, with really only when necessary to call, passing the main function, the typical functional programming ideas.
In JS, Thunk function replacement is not an expression, but multi-parameter function, it is replaced by a single parameter version, and only accept the callback function as an argument , in essence, currying function .
function Thunk(fn){ |
Thunk just not much use, it requires a combination Generator for process management.
Generator
Generator function returns an iterator Iterator objects, we can use this iterator traverses manually correlation value, the state, to ensure the correct order of execution. Generator function with a next method.
By next function, to yield the corresponding expression may be performed, and the next () function can also take parameter, which can be used as the expression returns a yield value, yield because there is no return value itself, if the next () does not parameters, the yield of each operation are in the return value is undefined
function* gen() { |
Generator letter big column Thunk function and Generator function number simply returns a pointer to the object inside the iteration, under normal circumstances, we need to take the initiative to trigger the next () is executed, so with only Generator is not suitable for asynchronous operation.
yield
The key generator function execution is suspended,yield
the value of the expression after the keyword returned to the caller to the generator. It can be considered a generator-based version ofreturn
the keyword.yield
Keywords actual return aIteratorResult
object that has two properties,value
anddone
.value
Property isyield
the result of expression evaluation, anddone
isfalse
showing a generator function has not been fully completed.If the generator is passed to the optional value
next()
method, the generator current value becomesyield
the value returned by the operation
Thunk automatic process management functions
Thunk function of real power, that can automatically execute Generator function
FUNCTION run ( fn ) { were gene = fn (); |
first next function pointer to a function Generator The next step (gen.next method), and then determines whether to end the function Generator (result.done attributes), if not finished, the next function reinjecting Thunk function (result.value Properties ), otherwise exit