Skip to content

Instantly share code, notes, and snippets.

@hasangilak
Last active January 18, 2017 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasangilak/bf5c4216283517ed10aa65d2fca1f0fb to your computer and use it in GitHub Desktop.
Save hasangilak/bf5c4216283517ed10aa65d2fca1f0fb to your computer and use it in GitHub Desktop.
import {sync} from "./sync";
import {handleError} from "./catchError";
export function raceOnTime(generator, timeOut) {
return (new Promise((resolve, reject) => {
let _resolve;
const timer = setTimeout(() => {
reject({error: new Error('timeout')});
}, timeOut);
const done = () => {
_resolve();
resolve({success: 'ok'});
clearTimeout(timer);
};
new Promise((resolve) => {
_resolve = resolve;
sync(function*() {
try {
yield* generator(done);
} catch (error) {
handleError(error);
}
});
}).then();
}));
}
// example
function* categoryListController(done) {
loading(true);
const {error, data} = yield (new swagger.CategoryApi())
.categoryListGet(select('user.token'), {
...select('queries.channel', {}),
def: true
});
done();
if (!error) {
dispatch(categoryListAction(data));
loading(false);
} else {
throwError("onCategoryEnterMiddleWare", function () {
navigate('/v1/login');
});
}
}
export default (nextState, replace, next) => sync(function*() {
try {
yield* isLoginMiddleware();
let {error} = yield raceOnTime(categoryListController, 20000);
if (error)
navigate('/v1/login');
next();
} catch (error) {
handleError(error);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment