From 5e93b71bf48b0450163cb642e80f24a1df81ef95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C?= Date: Sat, 27 Jan 2024 13:24:48 +0800 Subject: [PATCH] fix: PromiseLike bug: sync with Turbowarp Related pull request (merged): https://github.com/TurboWarp/scratch-vm/pull/185 --- src/compiler/jsexecute.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/compiler/jsexecute.js b/src/compiler/jsexecute.js index 8b444317..cb0dd036 100644 --- a/src/compiler/jsexecute.js +++ b/src/compiler/jsexecute.js @@ -116,19 +116,20 @@ const waitPromise = function*(promise) { const thread = globalState.thread; let returnValue; + // enter STATUS_PROMISE_WAIT and yield + // this will stop script execution until the promise handlers reset the thread status + // because promise handlers might execute immediately, configure thread.status here + thread.status = 1; // STATUS_PROMISE_WAIT + promise .then(value => { returnValue = value; thread.status = 0; // STATUS_RUNNING - }) - .catch(error => { + }, error => { thread.status = 0; // STATUS_RUNNING globalState.log.warn('Promise rejected in compiled script:', error); }); - // enter STATUS_PROMISE_WAIT and yield - // this will stop script execution until the promise handlers reset the thread status - thread.status = 1; // STATUS_PROMISE_WAIT yield; return returnValue;