#promise #javascript #thread #js #future

rsvow

A Rust-like implementation of JavaScript's Promise mechanism

2 releases

0.1.1 Feb 22, 2025
0.1.0 Feb 22, 2025

#262 in Concurrency

Download history 228/week @ 2025-02-19 47/week @ 2025-02-26

275 downloads per month

MIT/Apache

52KB
915 lines

Rsvow

A Rust-like implementation of JavaScript's Promise mechanism.

Overview

rsvow is a lightweight Rust crate that provides an API similar to JavaScript's Promise. It allows you to manage asynchronous or delayed computations with a familiar interface.

For more details, check out the documentation.

Features

  • Simple API: rsvow provides a simple and easy-to-use API that functions similarly to JavaScript's Promise.
  • Thread-safe: rsvow is thread-safe and can be used in multi-threaded environments.
  • Asynchronous: rsvow allows you to run code asynchronously and handle the results in a clean and concise way.
  • Error handling: rsvow provides error handling mechanisms that allow you to catch and handle errors in a convenient manner.
  • Chaining: rsvow supports chaining of promises, allowing you to create complex asynchronous workflows with ease.

Installation

Add the following dependency to your Cargo.toml:

[dependencies]
rsvow = "0.1.0"

Or if you want to use the latest version from the main branch:

[dependencies]
rsvow = { git = "https://github.com/ferranSanchezLlado/rsvow.git" }

Usage

Below is a simple example of using rsvow:

use rsvow::{Promise, State};

fn main() {
    // Create a promise that resolves immediately with a value.
    let promise: Promise<i32, ()> = Promise::resolve(42);

    // Chain a `then` callback to double the value.
    let doubled = promise.then(|value| value * 2);

    // Check the result.
    assert_eq!(doubled.state(), State::Fulfilled(84));
}

You can also run code asynchronously:

use rsvow::{Promise, State};
use std::thread;

fn main() {
    // Create a promise that immediately rejects.
    let promise: Promise<(), &str> = Promise::reject("failure");

    // Transform the error message.
    let error_handled = promise.catch(|error| {
        thread::spawn(move || {
            thread::sleep(std::time::Duration::from_millis(100));
            error(format!("Handled error: {}", error))
        })
    });

    assert_eq!(error_handled.state(), State::Pending);
    thread::sleep(std::time::Duration::from_millis(200));
    assert_eq!(error_handled.state(), State::Fulfilled("Handled error: failure"));
}

License

Dual-licensed under MIT or Apache-2.0. See LICENSE for details.

No runtime deps