Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mobx-async-action

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-async-action

MobX asynchronous action

  • 0.1.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

mobx-async-action

MobX asynchronous action but different with flow.

The reactions won't be triggered until the promise's state changes to be fulfilled or rejected.

Example

import { observable, reaction } from "mobx";
import { expect, test } from "vitest";

import { runInAsyncAction } from ".";

function wait(ms: number) {
	return new Promise((resolve) => setTimeout(resolve, ms));
}

test("runInAsyncAction", async () => {
	let hasReacted = false;

	const ob = observable({
		value: 1,
	});

	reaction(
		() => ob.value,
		() => {
			hasReacted = true;
		}
	);

	await runInAsyncAction(async () => {
		ob.value = 2;
		await wait(1000);
		expect(hasReacted).toBeFalsy();
	});

	expect(hasReacted).toBeTruthy();
});

Caveat

If the promise's state never changes, the action may never end.

API Reference

asyncAction

Similar to action, but supports asynchronous function.

function asyncAction<T>(fn: () => Promise<T>): () => Promise<T>;

function asyncAction<T>(
	actionName: string,
	fn: () => Promise<T>
): () => Promise<T>;

runInAsyncAction

Similar to runInAction, but use asyncAction underhood.

function runInAsyncAction(fn: () => Promise<void>): Promise<void>;

Keywords

FAQs

Package last updated on 31 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc