Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement