From 50411efc89fabb77c02b472446221e6bcd9ff621 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 18 Nov 2020 15:00:50 +0100 Subject: cancellable promise. wont cancel the current request, but at least breaks the chain --- animism-align/frontend/app/utils/image.utils.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'animism-align/frontend') 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) => ( -- cgit v1.2.3-70-g09d2