README

moonbitlang/async/js_async does not have a README file

#
JsError

#internal(unimplemented, "unimplemented in native backend")
type JsError

A JavaScript exception wrapped in MoonBit error
impl Show for JsError

#
AbortController

#internal(unimplemented, "unimplemented in native backend")
#external
pub type AbortController

A JavaScript AbortController used to cancel promises

#
AbortController::abort

#internal(unimplemented, "unimplemented in native backend")
fn AbortController::abort(self : AbortController) -> Unit

Abort the abort controller and activate its abort signal. All promises tied to the signal will be cancelled.

#
AbortController::new

#internal(unimplemented, "unimplemented in native backend")
fn AbortController::new() -> AbortController

Create a new abort controller

#
AbortController::signal

#internal(unimplemented, "unimplemented in native backend")
fn AbortController::signal(self : AbortController) -> AbortSignal

Get the abort signal of an abort controller. The abort signal will be triggered when .abort() is called on the controller.

#
AbortSignal

#internal(unimplemented, "unimplemented in native backend")
#external
pub type AbortSignal

A JavaScript AbortSignal used to cancel promises

#
JsReadableStream

#internal(unimplemented, "unimplemented in native backend")
type JsReadableStream

The ReadableStream type in Web API, see https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream for more details.

Use this type on JavaScript FFI boundary to interact with API that make use of ReadableStream, such as fetch.

#
JsReadableStream::new_pipe

#internal(unimplemented, "unimplemented in native backend")
fn JsReadableStream::new_pipe() -> (JsReadableStream,
PipeWrite
)

Create an anonymous pipe and wrap the read end into a JsReadableStream. This allows writing data from MoonBit code to JavaScript. Usually users should pass the read end of the pipe (JsReadableStream) to JavaScript code, and write to the write end of the pipe (@io.PipeWrite) from MoonBit code.

Closing the write end of the pipe will signal EOF on the read end. If the read end of the stream is cancelled from the JavaScript side, further write operations on the write end will fail with error.

#
Promise

#internal(unimplemented, "unimplemented in native backend")
#external
pub type Promise[X]

A JavaScript promise that resolves to a value of type X

#
Promise::from_async

#internal(unimplemented, "unimplemented in native backend")
fn[X] Promise::from_async(f : async () -> X, abort_signal? : AbortSignal) -> Promise[X]

Convert a MoonBit async function into a JavaScript promise. Promise::from_async(f) returns a promise that:

  • resolve to the result of f when f return
  • reject with the error that f raise if f fail

If abort_signal is present, f will be automatically cancelled when the signal is activated, and the returned promise will reject with JavaScript AbortError.

The async function f will be run in a global context, so there is no structured concurrency support for Promise::from_async, and this function should only be used for exporting MoonBit code to JavaScript.

It is undefined whether f will actually start running immediately, or start at the next JavaScript event loop.

#
Promise::wait

#internal(unimplemented, "unimplemented in native backend")
async fn[X] Promise::wait(promise : Promise[X], abort_controller? : AbortController) -> X

Wait for a JavaScript promise to resolve and return the fulfilled value, or raise an error if the promise is rejected.

If abort_controller is provided, it should be an abort controller that controls the promise, and .abort() will be called on the controller automatically when wait is cancelled, cancelling the promise automatically.

If abort_controller is absent, wait is non-cancellable.

#
ReadableStream

#internal(unimplemented, "unimplemented in native backend")
type ReadableStream

A wrapper around JsReadableStream that allows reading the content of the stream via the @io.Reader interface.

This type SHOULD NOT be used on FFI directly: its ABI is NOT the same as JsReadableStream, and most JavaScript types won't understand it.

#
ReadableStream::close

#internal(unimplemented, "unimplemented in native backend")
fn ReadableStream::close(self : ReadableStream) -> Unit

Close a ReadableStream. The underlying JS stream will be cancelled.

#
ReadableStream::from_js

#internal(unimplemented, "unimplemented in native backend")
fn ReadableStream::from_js(stream : JsReadableStream) -> ReadableStream

Create a ReadableStream wrapper from a JsReadableStream object, in order to read the content of the stream from the MoonBit side. The ownership of the JS stream will be transferred to the ReadableStream: other code cannot read the stream anymore.

#
run_promise

#internal(unimplemented, "unimplemented in native backend")
async fn[X] run_promise(f : (AbortSignal) -> Promise[X]) -> X

Convenient helper for calling async JavaScript code from MoonBit. run_promise(f) create a fresh abort signal, passed it to f, and wait for the promise that f returns. If f resolve to a value, run_promise return that value. If f is rejected with an error, run_promise raise that error. If run_promise is cancelled, the abort signal passed to f is activated, and the promise returned by f should be cancelled automatically.

run_promise should only be used for cancellable JavaScript code (i.e. the promised returned by f should properly handle the passed-in abort signal). For non-cancellable JavaScript code, use Promise::wait() directly.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io