Yield
Callstack Ends
Don't Start Function Until CurrentInstall
npm install --save cjs-yield
How It Works
Call Function Normally
; console; // output // > Hi! // > callstack ends
Yield Function Call
var _yield = ; ; console; // output // > callstack ends // > Hi!
Pass Yielded Function Arguments
; console; // output // > callstack ends // > Hi Dr. Nick!
; console; // output // > callstack ends // > Hi Archie and the Drells!
; console; // output // > callstack ends // > Hi Ed, Edd and Eddy!
Gotchas
this
1. Yield DOES NOT work with functions that internally use Internally, yield uses setTimeout.apply
to yield a given function, while making it easy to pass arguments to it. To make the API as simple and as pleasant as posible, setTimeout.apply
uses null as its default context. This is fine unless the function use this
keyword internally. Even if you avoid writing functions that use this
, you can't escape it since many native JavaScript array methods like Array.push
use it.
To yield a function that uses this
, put the troublesome function into a function that doesn't use this
and pass that new function to yield.
Eg.
var heroes = ; ; console; // > callstack ends // > heroes=[]
vs
var heroes = ; ; console; // > callstack ends // > heroes=[ 'Batman' ] { heroes; }