Skip to content

Commit

Permalink
perf(useStorageAsync,useFetch): replace setTimeout with Promise (#2917)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-Skyblue committed Apr 12, 2023
1 parent 0842aa2 commit 8bd638b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/core/useFetch/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ describe('useFetch', () => {
})

test('should throw error', async () => {
const error1 = await useFetch('https://example.com?status=400').execute(true).catch(err => err)
const error2 = await useFetch('https://example.com?status=600').execute(true).catch(err => err)
const options = { immediate: false }
const error1 = await useFetch('https://example.com?status=400', options).execute(true).catch(err => err)
const error2 = await useFetch('https://example.com?status=600', options).execute(true).catch(err => err)

expect(error1.name).toBe('Error')
expect(error1.message).toBe('Bad Request')
Expand Down
2 changes: 1 addition & 1 deletion packages/core/useFetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ export function useFetch<T>(url: MaybeComputedRef<string>, ...args: any[]): UseF
}

if (options.immediate)
setTimeout(execute, 0)
Promise.resolve().then(() => execute())

return {
...shell,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/useStorageAsync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function useStorageAsync<T extends(string | number | boolean | object | n
read()

if (window && listenToStorageChanges)
useEventListener(window, 'storage', e => setTimeout(() => read(e), 0))
useEventListener(window, 'storage', e => Promise.resolve().then(() => read(e)))

if (storage) {
watchWithFilter(
Expand Down

0 comments on commit 8bd638b

Please sign in to comment.