Skip to content

Instantly share code, notes, and snippets.

View dtothefp's full-sized avatar

David Fox-Powell dtothefp

  • Flexport
  • United States
View GitHub Profile
const fromSpawn = spawn(function *() {
let someVal;
try {
//reject a promise
someVal = yield new Promise((res, rej) => {
setTimeout(rej.bind(null, 'whatevs!!!'), 1000);
});
} catch (err) {
export default function (dataBindings) {
@provideReactor(dataBindings || {})
@nuclearComponent((props) => {
const {Getters, modalId} = props;
return {
modalState: [...Getters.modalState, modalId]
}
})
return class Modal extends Component {
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) => {
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);
/**
* 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;
@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) => {
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);
}
});
/**
* 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
@dtothefp
dtothefp / thunk.js
Last active December 6, 2015 02:55
/**
* 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
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 );