diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2020-11-18 15:00:50 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2020-11-18 15:00:50 +0100 |
| commit | 50411efc89fabb77c02b472446221e6bcd9ff621 (patch) | |
| tree | e3b553e21e6a290462f2e23798c9e29d93dc438a | |
| parent | c43adc8a374aaa057a090e99161afba9bbc38286 (diff) | |
cancellable promise. wont cancel the current request, but at least breaks the chain
| -rw-r--r-- | animism-align/frontend/app/utils/image.utils.js | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/animism-align/frontend/app/utils/image.utils.js b/animism-align/frontend/app/utils/image.utils.js index dced2d8..d7b7508 100644 --- a/animism-align/frontend/app/utils/image.utils.js +++ b/animism-align/frontend/app/utils/image.utils.js @@ -4,14 +4,15 @@ export const preloadImages = urls => batchPromise(urls, 4, preloadImage) export const batchPromise = (list, count, fn) => { // console.log(list, count, fn) - return new Promise((resolve, reject) => { + let cancelled = false + const promise = new Promise((resolve, reject) => { let index = 0 let len = list.length const worker = j => ( new Promise(resolveWorker => { const next = () => { const i = index++ - if (i >= len) { + if (cancelled || i >= len) { return resolveWorker() } const item = list[i] @@ -34,6 +35,10 @@ export const batchPromise = (list, count, fn) => { .then(resolve) .catch(reject) }) + const cancel = () => cancelled = true + return { + promise, cancel + } } export const preloadImage = (url, anonymous=false) => ( |
