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

effects-as-data

Package Overview
Dependencies
Maintainers
1
Versions
155
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

effects-as-data

A micro abstraction layer for Javascript that makes writing, testing, and monitoring side-effects easy.

  • 3.0.15
  • beta
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8
increased by60%
Maintainers
1
Weekly downloads
 
Created
Source

Effects-as-data Example

const { promisify } = require("util");
const fs = require("fs");
const readFile = promisify(fs.readFile);
const logger = require("logger");

async function getResource(id) {
  const time = Date.now();
  const resource = await http.get(
    `http://example.com/resource/${id}?cache=${time}`
  );
  return getResource;
}

module.exports = {
  getResource
};

This is a drop-in replacement written using effects-as-data:

const { promisify, cmds } = require("effects-as-data");
const http = require("effects-as-data-http");

function* getResource(id) {
  const time = yield cmds.now();
  const resource = yield http.get(
    `http://example.com/resource/${id}?cache=${time}`
  );
  return getResource;
}

module.exports = {
  getResource: promisify(getResource)
};

What normally requires mocks, spies, and other tricks for unit testing, effects-as-data does simply and declaratively. The tests below tests all code branches in the function, tests the order in which side-effects occur and tests that everything is called the expected number of times and with the expected arguments:

const { testFn, args } = require("effects-as-data/test");
const { promisify, cmds } = require("effects-as-data");
const http = require("effects-as-data-http");

const testGetResource = testFn(getResource);

test(
  "getResource() should do an http get request and return the resource",
  testGetResource(() => {
    const id = "123";
    const time = 23456;
    const resource = { foo: "bar" };
    return args(id)
      .cmd(cmds.now())
      .result(time)
      .cmd(http.get(`http://example.com/resource/${id}?cache=${time}`))
      .result(resource)
      .returns(resource);
  })
);

Why Generators? Why not async/await?

Generators are much more powerful than async/await because generators allow developers to handle Javascript's most difficult problems all in one construct: asynchronous operations, non-determinism (ex: Date.now()), and eliminate in most code the use of globals, singletons, closures for state, dependency injection, brittle promise chains, and mocks/spies. Generators, when used in effects-as-data, allow developers to eliminate the anti-patterns that make Javascript hard to write, maintain and test.

FAQs

Package last updated on 09 May 2018

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