Skip to content

Commit

Permalink
chore: don't call cancel if there's no promise
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed Aug 15, 2020
1 parent 75955ac commit df560fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/vue-composable/__tests__/web/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,22 @@ describe("fetch", () => {
destroy();
expect(isCancelled.value).toBe(true);
});

it("should not cancel on unmount if there's no promise", () => {
let isCancelled: Ref<boolean> = undefined as any;

const { mount, destroy } = createVue({
template: `<div></div>`,
setup() {
isCancelled = useFetch().isCancelled;
}
});

mount();

expect(isCancelled.value).toBe(false);

destroy();
expect(isCancelled.value).toBe(false);
});
});
6 changes: 5 additions & 1 deletion packages/vue-composable/src/web/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ export function useFetch<T = any>(
}

if (unmountCancel && getCurrentInstance()) {
onUnmounted(() => cancel("unmounted"));
onUnmounted(() => {
if (abortController) {
cancel("unmounted");
}
});
}

return {
Expand Down

0 comments on commit df560fc

Please sign in to comment.