This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fromSpawn = spawn(function *() { | |
let someVal; | |
try { | |
//reject a promise | |
someVal = yield new Promise((res, rej) => { | |
setTimeout(rej.bind(null, 'whatevs!!!'), 1000); | |
}); | |
} catch (err) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function (dataBindings) { | |
@provideReactor(dataBindings || {}) | |
@nuclearComponent((props) => { | |
const {Getters, modalId} = props; | |
return { | |
modalState: [...Getters.modalState, modalId] | |
} | |
}) | |
return class Modal extends Component { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fromSpawn = spawn(function *() { | |
const someVal = yield new Promise((res, rej) => { | |
setTimeout(res.bind(null, 'whatevs!!!'), 5000); | |
}); | |
console.log(someVal); | |
return `I decided to return => ${someVal}`; | |
}); | |
fromSpawn.then((data) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fromSpawn = spawn(function *() { | |
const someVal = yield new Promise((res, rej) => { | |
setTimeout(res.bind(null, 'whatevs!!!'), 5000); | |
}); | |
console.log(someVal); | |
}); | |
fromSpawn.then((data) => { | |
console.log('Data from spawn', data); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Create a `spawn` function to deal with promises | |
* http://blog.mgechev.com/2014/12/21/handling-asynchronous-calls-with-es6-javascript-generators/ | |
*/ | |
function spawn(gen) { | |
const it = gen(); //instantiate the generator and return the generator object | |
function _co(method, arg) { | |
let res; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@provideContextAsProps({ | |
Actions: PropTypes.object.isRequired, | |
Getters: PropTypes.object.isRequired, | |
moreStuff: PropTypes.object, | |
stuffFromParent: PropTypes.func.isRequired | |
}) | |
@nuclearComponent((props) => { | |
const {someChildProps, stuffFromParent, Getters, Actions} = props; | |
stuffFromParent(someChildProps, (arg) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run(function *() { | |
const thunkedRead = thunk(fs.readFile); | |
try { | |
const contents = yield thunkedRead('/path/to/my/dope/file.js'); | |
console.log(contents.toString()); | |
} catch (err) { | |
console.log('Uh Oh', err); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Go one step further and create a `run` function that takes a `generator` function | |
* as it's only argument | |
* https://strongloop.com/strongblog/how-to-generators-node-js-yield-use-cases/ | |
* | |
* a) `run` caches the generator object | |
* b) contains a private `_next` function that initially starts the generator | |
* c) `_next` also acts as the callback to the Node callback signature (i.e. (err, data) => {}) | |
* - first time around it obtains the "thunkified" function from `yield` | |
* - `_next` is then called recursively by the "thunked" function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Thunk function returns a function that takes all args except the last `cb` that will return `err` or `data | |
* @param {Function} fn the function to wrap. ex. fs.readFile | |
* @param {Object|undefined} ctx the context to call the original `fn` with | |
* @return {Function} first function to be called with all args except the `cb` | |
* ex. const read = thunk(fs.readFile); | |
* const stuffFromTheCb = yield read('/path/to/cool/stuff.js'); | |
* | |
* ex. const cp = child_process.spawn('./path/to/some/binary', ['--cool', 'stuff']); | |
* const cpEventThunked = thunk(cp.on, cp); //pass the child process as context or it breaks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function *foo(x) { | |
var y = 2 * (yield (x + 1)); | |
var z = yield (y / 3); | |
return (x + y + z); | |
} | |
//this "instantiates" (not sure if this is correct??) the generator | |
//and returns an object with a `next` method | |
var it = foo( 5 ); |