-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Add Promise support to async route functions #3719
Changes from all commits
b94ebf6
e418beb
3f0fa6e
07ef4b7
a717634
8b26d30
1ebbf58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function isPromise(obj) { | ||
return obj && typeof obj.then === 'function' | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { loopAsync } from './AsyncUtils' | ||
import { isPromise } from './PromiseUtils' | ||
import { matchPattern } from './PatternUtils' | ||
import warning from './routerWarning' | ||
import { createRoutes } from './RouteUtils' | ||
|
@@ -18,7 +19,7 @@ function getChildRoutes(route, location, paramNames, paramValues, callback) { | |
params: createParams(paramNames, paramValues) | ||
} | ||
|
||
route.getChildRoutes(partialNextState, (error, childRoutes) => { | ||
const childRoutesReturn = route.getChildRoutes(partialNextState, (error, childRoutes) => { | ||
childRoutes = !error && createRoutes(childRoutes) | ||
if (sync) { | ||
result = [ error, childRoutes ] | ||
|
@@ -28,6 +29,13 @@ function getChildRoutes(route, location, paramNames, paramValues, callback) { | |
callback(error, childRoutes) | ||
}) | ||
|
||
if (isPromise(childRoutesReturn)) | ||
childRoutesReturn | ||
.then( | ||
childRoutes => callback(null, createRoutes(childRoutes)), | ||
callback | ||
) | ||
|
||
sync = false | ||
return result // Might be undefined. | ||
} | ||
|
@@ -41,9 +49,16 @@ function getIndexRoute(route, location, paramNames, paramValues, callback) { | |
params: createParams(paramNames, paramValues) | ||
} | ||
|
||
route.getIndexRoute(partialNextState, (error, indexRoute) => { | ||
const indexRoutesReturn = route.getIndexRoute(partialNextState, (error, indexRoute) => { | ||
callback(error, !error && createRoutes(indexRoute)[0]) | ||
}) | ||
|
||
if (isPromise(indexRoutesReturn)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder what we should do in cases like this where the function returns a non-promise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably what we do right now: Nothing. We can treat them as thunks, but why would anyone realistically do that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've actually used that pattern to return a slightly different component resolved synchronously based on the query. I just invoked the cb synchronously though. Heh. |
||
indexRoutesReturn | ||
.then( | ||
indexRoute => callback(null, createRoutes(indexRoute)[0]), | ||
callback | ||
) | ||
} else if (route.childRoutes) { | ||
const pathless = route.childRoutes.filter(childRoute => !childRoute.path) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dunno if this is worth throwing in a module. Maybe move it to AsyncUtils at least?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is okay for now I think.