Estoy tratando de crear algún tipo de aplicación de referencia que ejecute simultáneamente el módulo WASM y su función JS equivalente y luego compare el tiempo de ejecución de esos dos. Intenté usar promesas, devoluciones de llamadas y el ejemplo de MDN con el uso de await Promise.all(arrayOfPromises) , pero en cada caso, la función JS comienza a ejecutarse después de que finaliza el módulo WASM.
El código está abajo, ignore el modelo DOM:
import { default as wasm } from "./pkg/hello_world.js" import { sortRandomArray } from "./src/sorting.js" window.onload = () => { const btn = document.getElementById("inputConfirmation"); addListeners(Array(btn), runTestAndPrintResult); } async function runWasmModule(sortRandomArray, arraySize) { return new Promise(() => { const time = parseFloat(sortRandomArray(arraySize)); console.log('wasm done ' + time); }); } async function runJSAlgo(sortRandomArray, arraySize) { return new Promise(() => { const time = sortRandomArray(arraySize) / 1000; console.log('js done ' + time); }); } function addListeners(elements, fun) { for (const element of elements) { element.addEventListener("click", fun); } document.addEventListener("keypress", event => { if (event.key === "Enter") { fun(); } }) } const runTestAndPrintResult = async () => { let arraySize = document.getElementById("arraySizeInput").value; if (arraySize === "") { return; } arraySize = parseInt(arraySize); const n = document.createElement("h1"); if (arraySize <= 1) { n.textContent = "Nice try :)"; document.body.appendChild(n); return; } const wasmInstance = wasm(); wasmInstance.then(async (module) => { await Promise.all([ runWasmModule(module.sort_random_array, arraySize), runJSAlgo(sortRandomArray, arraySize), ]); }).catch(err => { console.error(err); }) }Entiendo que JS es un lenguaje de subproceso único, pero me confundí con todos los términos en línea sobre la naturaleza asíncrona. Si no hay forma de hacer esto en JS, ¿hay algo más que pueda intentar para realizar esto?