Skip to content

Commit

Permalink
convert promise logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot committed Jul 18, 2020
1 parent d67b441 commit 6b31288
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ export default class Router implements BaseRouter {
as: string,
options: any
): Promise<boolean> {
return new Promise((resolve, reject) => {
return Promise.resolve().then(() => {
if (!options._h) {
this.isSsr = false
}
Expand Down Expand Up @@ -477,12 +477,12 @@ export default class Router implements BaseRouter {
this.changeState(method, url, as, options)
this.scrollToHash(cleanedAs)
Router.events.emit('hashChangeComplete', cleanedAs)
return resolve(true)
return true
}

const parsed = tryParseRelativeUrl(url)

if (!parsed) return resolve(false)
if (!parsed) return false

let { pathname, searchParams } = parsed
const query = searchParamsToUrlQuery(searchParams)
Expand Down Expand Up @@ -525,11 +525,9 @@ export default class Router implements BaseRouter {
)
}

return reject(
new Error(
`The provided \`as\` value (${asPathname}) is incompatible with the \`href\` value (${route}). ` +
`Read more: https://err.sh/vercel/next.js/incompatible-href-as`
)
throw new Error(
`The provided \`as\` value (${asPathname}) is incompatible with the \`href\` value (${route}). ` +
`Read more: https://err.sh/vercel/next.js/incompatible-href-as`
)
}
} else {
Expand All @@ -541,18 +539,18 @@ export default class Router implements BaseRouter {
Router.events.emit('routeChangeStart', cleanedAs)

// If shallow is true and the route exists in the router cache we reuse the previous result
this.getRouteInfo(route, pathname, query, as, shallow).then(
return this.getRouteInfo(route, pathname, query, as, shallow).then(
(routeInfo) => {
const { error } = routeInfo

if (error && error.cancelled) {
// An event already has been fired
return resolve(false)
return false
}

if (error && error[ABORTED]) {
Router.events.emit('routeChangeError', error, cleanedAs)
return resolve(false)
return false
}

Router.events.emit('beforeHistoryChange', cleanedAs)
Expand All @@ -565,23 +563,24 @@ export default class Router implements BaseRouter {
!(routeInfo.Component as any).getInitialProps
}

this.set(route, pathname!, query, cleanedAs, routeInfo).then(() => {
if (error) {
Router.events.emit('routeChangeError', error, cleanedAs)
throw error
}
return this.set(route, pathname!, query, cleanedAs, routeInfo).then(
() => {
if (error) {
Router.events.emit('routeChangeError', error, cleanedAs)
throw error
}

if (process.env.__NEXT_SCROLL_RESTORATION) {
if (manualScrollRestoration && '_N_X' in options) {
window.scrollTo(options._N_X, options._N_Y)
if (process.env.__NEXT_SCROLL_RESTORATION) {
if (manualScrollRestoration && '_N_X' in options) {
window.scrollTo(options._N_X, options._N_Y)
}
}
}
Router.events.emit('routeChangeComplete', cleanedAs)
Router.events.emit('routeChangeComplete', cleanedAs)

return resolve(true)
})
},
reject
return true
}
)
}
)
})
}
Expand Down

0 comments on commit 6b31288

Please sign in to comment.