Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronous operation #163

Closed
1 of 3 tasks
futpib opened this issue Feb 18, 2019 · 3 comments · Fixed by #164
Closed
1 of 3 tasks

Synchronous operation #163

futpib opened this issue Feb 18, 2019 · 3 comments · Fixed by #164

Comments

@futpib
Copy link
Contributor

futpib commented Feb 18, 2019

I'm submitting a ...

  • bug report
  • feature request
  • other (Please do not submit support requests here (below))

Sometimes a third party api forces you to run synchronously (this was the reason I could not integrate universal-router into my project). Or you want to just keep it simple or avoid extra unnecessary ticks.

Since consumers may already expect that resolve always returns a promise, this may be implemented by adding a resolveSync method.

@frenzzy
Copy link
Member

frenzzy commented Feb 18, 2019

@futpib hi, could you please show an example when/how it could be useful?

Or you want to just keep it simple or avoid extra unnecessary ticks.

Btw, do you know that promises are executed in the current event loop (tick) on a microtask queue?

@futpib
Copy link
Contributor Author

futpib commented Feb 18, 2019

could you please show an example when/how it could be useful?

I find myself writing an application inside a proprietary app container that expects that frontend code (which started as a simple template, which ran synchronously) to return html string synchronously. The code now contains a bunch of ifs testing the request path and begs for a router.

Btw, do you know that promises are executed in the current event loop (tick) on a microtask queue?

It's a queue anyway, right? Let's call ticks on the microtask queue microticks if this lessens confusion. Do you think microtask queue does not inflict overhead compared to synchronous code?

@frenzzy
Copy link
Member

frenzzy commented Feb 18, 2019

Ok, I think we're pretty much on the same page.

I don't want to add resolveSync method into the core because it will increase library size with not profit for most of the cases since loading a page usually is asynchronous process. I also believe that any web app could be designed with this in mind and maybe it is better to change design of your app earlier than later.

But I agree that synchronous router could be useful in some certain cases. What do you think about an add-on approach like we already have with url generation? For example something like:

import UniversalRouter from 'universal-router'
import resolveSync from 'universal-router/resolveSync'

const router = new UniversalRouter(routes)

resolveSync(router, pathnameOrContext) // => result

or

import UniversalRouter from 'universal-router'
import withResolveSync from 'universal-router/withResolveSync'

const Router = withResolveSync(UniversalRouter)
const router = new Router(routes)

router.resolveSync(pathnameOrContext) // => result

or

import UniversalRouter from 'universal-router/sync'

const router = new UniversalRouter(routes)

router.resolve(pathnameOrContext) // => result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants