# Introducing the WebAssembly JavaScript Promise Integration API

DevFeed: [Introducing the WebAssembly JavaScript Promise Integration API](<https://devfeed.tech/articles/introducing-the-webassembly-javascript-promise-integration-api-3526.md>)

Original publisher: [Read original article](<https://v8.dev/blog/jspi>)

Author: Francis McCabe, Thibaud Michaud, Ilya Rezvov, Brendan Dahl

Published: 2024-07-01T00:00:00Z

Content type: tutorial

Language: en

Sources: [V8](<https://devfeed.tech/sources/v8.md>)

Topics: [WebAssembly](<https://devfeed.tech/topics/web-assembly.md>), [API](<https://devfeed.tech/topics/api.md>), [Promise](<https://devfeed.tech/topics/promise.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>), [Web APIs](<https://devfeed.tech/topics/web-apis.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [apis](<https://devfeed.tech/tags/apis.md>), [code](<https://devfeed.tech/tags/code.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [web](<https://devfeed.tech/tags/web.md>), [web-apis](<https://devfeed.tech/tags/web-apis.md>), [webassembly](<https://devfeed.tech/tags/webassembly.md>)

## AI overview

This article introduces the WebAssembly JavaScript Promise Integration (JSPI) API. JSPI enables WebAssembly applications designed around synchronous APIs to work with asynchronous JavaScript and Web APIs by suspending execution during asynchronous operations and resuming it when they complete. The approach requires few changes to the WebAssembly application and is illustrated with development guidance and examples.

## Source excerpt

The JavaScript Promise Integration (JSPI) API allows WebAssembly applications that were written assuming synchronous access to external functionality to operate smoothly in an environment where the functionality is actually asynchronous. This note outlines what the core capabilities of the JSPI API are, how to access it, how to develop software for it and offers some examples to try out. What is 'JSPI' for? # Asynchronous APIs operate by separating the initiation of the operation from its resolution; with the latter coming some time after the first. Most importantly, the application continues execution after kicking off the operation; and is then notified when the operation completes. For example, using the fetch API, Web applications can access the contents associated with a URL; however, the fetch function does not directly return the results of the fetch; instead it returns a Promise object. The connection between the fetch response and the original request is reestablished by attaching a callback to that Promise object. The callback function can inspect the response and collect the data (if it is there of course). On the other hand, many cases C/C++ (and many other languages) applications are originally written against a synchronous API. For example, the Posix read function does not complete until the I/O operation is complete: the read function blocks until the read is complete. However, it is not permitted to block the browser's main thread; and many environments are not supportive of synchronous programming. The result is a mismatch between the desires of the application programmer for a simple to use API and the wider ecosystem that requires I/O to be crafted with asynchronous code. This is especially a problem for existing legacy applications that would be expensive to port. The JSPI is an API that bridges the gap between synchronous applications and asynchronous Web APIs. It works by intercepting Promise objects returned by asynchronous Web API functions a