Skip to content

Instantly share code, notes, and snippets.

const fromSpawn = spawn(function *() {
const someVal = yield new Promise((res, rej) => {
setTimeout(res.bind(null, 'whatevs!!!'), 1000);
});
bloooBlaahhh
});
fromSpawn.then((data) => {
console.log('Data from spawn', data);
const fromSpawn = spawn(function *() {
let rejValFirst, rejValSecond, resValLast;
try {
rejValFirst = yield new Promise((res, rej) => {
setTimeout(rej.bind(null, 'blaaa...first rejected!!!'), 1000);
});
} catch (err) {
console.error('Inside spawn error', err);
function spawn(gen) {
const it = gen();
function _co(method, arg) {
let res;
try {
console.log('***TRY***', method, arg);
res = it[method](arg);
} catch(err) {
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);
}
});