diff --git a/src/core/classes/prism.ts b/src/core/classes/prism.ts index 14b93b2a84..3b21b4afa5 100644 --- a/src/core/classes/prism.ts +++ b/src/core/classes/prism.ts @@ -73,7 +73,6 @@ export default class Prism { if (!this.config.manual) { this.waitFor.push(documentReady()); - this.ready = allSettled(this.waitFor); this.ready .then(() => { this.highlightAll(); diff --git a/src/util/async.ts b/src/util/async.ts index f5b8804223..d0493c8150 100644 --- a/src/util/async.ts +++ b/src/util/async.ts @@ -39,9 +39,19 @@ export function nextTick () { }); } +// In addition to waiting for all promises to settle, handle post-hoc additions/removals. export async function allSettled (promises: Promise[]): Promise<(T | null)[]> { - const outcomes = await Promise.allSettled(promises); - return outcomes.map(o => (o.status === 'fulfilled' ? o.value : null)); + return Promise.allSettled(promises).then(outcomes => { + if (promises.length > 0 && promises.length !== outcomes.length) { + // The list of promises changed. Return a new Promise. + // The original promise won't resolve until the new one does. + return allSettled(promises); + } + + // The list of promises either empty or stayed the same. + // Return results immediately. + return outcomes.map(o => (o.status === 'fulfilled' ? o.value : null)); + }); } export class Deferred extends Promise {