Skip to content

Commit

Permalink
Prefer promiseCallback.resolve where possible.
Browse files Browse the repository at this point in the history
`promiseCallback.resolve()` is implemented as `cb(); return promise`,
so doing `promiseCallback.callback(); return promiseCallback.promise`
is unnecessary.
  • Loading branch information
wjhsf committed Nov 5, 2023
1 parent 030e176 commit 09d2433
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions lib/memstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ export class MemoryCookieStore extends Store {

const results: Cookie[] = []
const promiseCallback = createPromiseCallback<Cookie[]>(callback)
const cb = promiseCallback.callback

if (!domain) {
return promiseCallback.resolve([])
Expand Down Expand Up @@ -174,20 +173,17 @@ export class MemoryCookieStore extends Store {
pathMatcher(domainIndex)
})

cb(null, results)
return promiseCallback.promise
return promiseCallback.resolve(results)
}

override putCookie(cookie: Cookie): Promise<void>
override putCookie(cookie: Cookie, callback: ErrorCallback): void
override putCookie(cookie: Cookie, callback?: ErrorCallback): unknown {
const promiseCallback = createPromiseCallback<undefined>(callback)
const cb = promiseCallback.callback

const { domain, path, key } = cookie
if (domain == null || path == null || key == null) {
cb(null, undefined)
return promiseCallback.promise
return promiseCallback.resolve(undefined)
}

const domainEntry =
Expand All @@ -204,9 +200,7 @@ export class MemoryCookieStore extends Store {

pathEntry[key] = cookie

cb(null, undefined)

return promiseCallback.promise
return promiseCallback.resolve(undefined)
}

override updateCookie(oldCookie: Cookie, newCookie: Cookie): Promise<void>
Expand Down Expand Up @@ -246,10 +240,8 @@ export class MemoryCookieStore extends Store {
callback?: ErrorCallback,
): unknown {
const promiseCallback = createPromiseCallback<undefined>(callback)
const cb = promiseCallback.callback
delete this.idx[domain]?.[path]?.[key]
cb(null, undefined)
return promiseCallback.promise
return promiseCallback.resolve(undefined)
}

override removeCookies(domain: string, path: string): Promise<void>
Expand All @@ -264,7 +256,6 @@ export class MemoryCookieStore extends Store {
callback?: ErrorCallback,
): unknown {
const promiseCallback = createPromiseCallback<undefined>(callback)
const cb = promiseCallback.callback

const domainEntry = this.idx[domain]
if (domainEntry) {
Expand All @@ -275,8 +266,7 @@ export class MemoryCookieStore extends Store {
}
}

cb(null, undefined)
return promiseCallback.promise
return promiseCallback.resolve(undefined)
}

override removeAllCookies(): Promise<void>
Expand Down

0 comments on commit 09d2433

Please sign in to comment.