# WASM version engine support

**URL:** https://swi-prolog.discourse.group/t/wasm-version-engine-support/8771
**Category:** Announce
**Tags:** wasm, threads, engines
**Created:** [February 6, 2025, 1:09pm UTC](https://swi-prolog.discourse.group/t/wasm-version-engine-support/8771 "2025-02-06T13:09:09Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)
#### Post date: [February 6, 2025, 1:09pm UTC](https://swi-prolog.discourse.group/t/wasm-version-engine-support/8771/1 "2025-02-06T13:09:09Z")

</div>

I have added support the _engines_ to the WASM version as well as by default enabled engine support in the _single threaded_ version. Engines can be used for implementing coroutining (not to be confused with this term as it is used in the context of _constraints_).

- `Prolog.query()` has an additional `options` parameter that can be used to run the query in a separate engine. See below. This way, JavaScript can open multiple queries and generate answers from them in any order.

- `Prolog.forEach()` also got this option. As this function return a `Promise`, we can create a thread using code as below. The new engine executes my\_goal(X) and calls handle(X) on each answer. By default it yields back the the browser event loop every 10k inferences, allowing other tasks to proceed.

```prolog
setTimeout(async () => {
  await Prolog.forEach("my_goal(X)",
			           (a) => handle(a.X)),
			           {engine:true});
});

```

Enjoy the demos at [SWI-Prolog WASM demos](https://wasm.swi-prolog.org/wasm/) You can use the browser to see the source or download the git repo and check out the `src/wasm` directory.

As is, everything uses the initial engine by default. I wonder whether or not it is a good idea to use a fresh engine by default for several tasks. The [SWI-Prolog WASM benchmarks](https://wasm.swi-prolog.org/wasm/bench.html) indicate that creating and destroying engines is fast (~8μs)
