From 015e5ebd63beba65287f1c8e1ce9a4aafffc7510 Mon Sep 17 00:00:00 2001 From: GarboMuffin Date: Sat, 12 Nov 2022 00:24:00 -0600 Subject: [PATCH] Extract isPromise out of executeInCompatibilityLayer and clarify some jsdoc --- src/compiler/jsexecute.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/compiler/jsexecute.js b/src/compiler/jsexecute.js index a7751ffc..f4cbe611 100644 --- a/src/compiler/jsexecute.js +++ b/src/compiler/jsexecute.js @@ -84,13 +84,19 @@ runtimeFunctions.waitThreads = `const waitThreads = function*(threads) { }`; /** - * Wait until a Promise resolves or rejects before continuing. + * waitPromise: Wait until a Promise resolves or rejects before continuing. * @param {Promise} promise The promise to wait for. * @returns {*} the value that the promise resolves to, otherwise undefined if the promise rejects */ /** - * Execute a scratch-vm primitive. + * isPromise: Determine if a value is Promise-like + * @param {unknown} promise The value to check + * @returns {promise is PromiseLike} True if the value is Promise-like (has a .then()) + */ + +/** + * executeInCompatibilityLayer: Execute a scratch-vm primitive. * @param {*} inputs The inputs to pass to the block. * @param {function} blockFunction The primitive's function. * @param {boolean} useFlags Whether to set flags (hasResumedFromPromise) @@ -118,6 +124,12 @@ const waitPromise = function*(promise) { return returnValue; }; +const isPromise = value => ( + // see engine/execute.js + value !== null && + typeof value === 'object' && + typeof value.then === 'function' +); const executeInCompatibilityLayer = function*(inputs, blockFunction, isWarp, useFlags) { const thread = globalState.thread; @@ -131,13 +143,6 @@ const executeInCompatibilityLayer = function*(inputs, blockFunction, isWarp, use return blockFunction(inputs, blockUtility); }; - const isPromise = value => ( - // see engine/execute.js - value !== null && - typeof value === 'object' && - typeof value.then === 'function' - ); - let returnValue = executeBlock(); if (isPromise(returnValue)) {