forked from pikax/vue-composable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcancellablePromise.html
77 lines (72 loc) · 1.83 KB
/
cancellablePromise.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.config.devtools = true;
</script>
</head>
<body>
<div id="app">
<div>
<label for="delayPromise">
Delay seconds
</label>
<input name="delayPromise" v-model="delay" />
</div>
<div>
<button @click="exec(delay)" :disabled="loading">Execute</button>
<button @click="cancel()" :disabled="!loading">
Cancel
</button>
</div>
<div v-if="loading">
loading...
</div>
<div v-else-if="cancelled">
<p>Request cancelled</p>
<p>Result: {{ result }}</p>
<p>Error: {{ error }}</p>
</div>
<div v-else>
<p>Result: {{ result }}</p>
<p>Error: {{ error }}</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/@vue/[email protected]/dist/vue-composition-api.umd.js"></script>
<script>
// add composition api to vue
Vue.use(vueCompositionApi.default);
</script>
<script src="../dist/vue-composable.umd.js"></script>
<script>
const { useCancellablePromise } = vueComposable;
const { computed, ref, watch } = vueCompositionApi;
new Vue({
el: "#app",
setup() {
const {
exec,
loading,
cancel,
error,
cancelled,
result
} = useCancellablePromise(delay =>
fetch(`https://reqres.in/api/users?delay=${delay}`)
.then(x => x.json())
);
const delay = ref(1);
return {
delay,
exec,
error,
loading,
cancel,
cancelled,
result
};
}
});
</script>
</body>
</html>