Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80551 views
1
/**
2
* Accepts an array of matched routes as returned from react-router's
3
* `Router.run()` and calls the given static method on each. The methods may
4
* return a promise.
5
*
6
* Returns a promise that resolves after any promises returned by the routes
7
* resolve. The practical uptake is that you can wait for your data to be
8
* fetched before continuing. Based off react-router's async-data example
9
* https://github.com/rackt/react-router/blob/master/examples/async-data/app.js#L121
10
* @param {array} routes - Matched routes
11
* @param {string} methodName - Name of static method to call
12
* @param {...any} ...args - Arguments to pass to the static method
13
*/
14
export default async function performRouteHandlerStaticMethod(routes, methodName, ...args) {
15
return Promise.all(routes
16
.map(route => route.handler[methodName])
17
.filter(method => typeof method === 'function')
18
.map(method => method(...args))
19
);
20
}
21
22