Skip to content

Commit 9364b2f

Browse files
authored
Prevent ResourceManager test failures due to unhandled rejection… (#2812)
* fix: prevent ResourceManager test failures due to unhandled rejection
1 parent bf7da35 commit 9364b2f

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

packages/core/src/asset/AssetPromise.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,23 @@ export class AssetPromise<T> implements PromiseLike<T> {
209209
}
210210

211211
/**
212-
* Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The
213-
* resolved value cannot be modified from the callback.
214-
* @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected).
215-
* @returns A Promise for the completion of the callback.
212+
* Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected).
213+
* The callback result is ignored and the original value/reason is preserved per Promise spec.
214+
* Returns an AssetPromise to keep chainability with AssetPromise methods.
215+
* @param onFinally - The callback to execute when the Promise is settled.
216+
* @returns An AssetPromise for the completion of the callback.
216217
*/
217-
finally(onFinally?: () => void): Promise<T> {
218-
return this._promise.finally(onFinally);
218+
finally(onFinally?: () => void): AssetPromise<T> {
219+
return this.then(
220+
(value) => {
221+
onFinally?.();
222+
return value;
223+
},
224+
(reason) => {
225+
onFinally?.();
226+
throw reason;
227+
}
228+
);
219229
}
220230

221231
/**

packages/loader/src/gltf/parser/GLTFParserContext.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,13 @@ export class GLTFParserContext {
158158
_addTaskCompletePromise(taskPromise: AssetPromise<any>): void {
159159
const task = this._progress.taskComplete;
160160
task.total += 1;
161-
taskPromise.finally(() => {
162-
this._setTaskCompleteProgress(++task.loaded, task.total);
163-
});
161+
taskPromise
162+
.finally(() => {
163+
this._setTaskCompleteProgress(++task.loaded, task.total);
164+
})
165+
.catch((e) => {
166+
// Need catch to avoid unhandled rejection
167+
});
164168
}
165169

166170
private _handleSubAsset<T>(

0 commit comments

Comments
 (0)