npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

asyncresult-js

v1.1.3

Published

helpful wrapper for promises

Downloads

5

Readme

asyncresult-js

Small set of utils for working with promises.

version Coverage Status Build status

examples:

usual promise approach:

fetchSomething()
  .then(() => {
    fetchSomethingElse()
      .then(() => {
        fetchLastThing()
          .then(data => {
            showTheData(data);
          })
          .catch(err => {
            throw err;
          });
      })
      .catch(err => {
        throw err;
      });
  })
  .catch(err => {
    throw err;
  });

usual async/await approach

// suppose you are in some async context
async () => {
  try {
    await fetchSomething();
  } catch (err) {
    throw err;
  }

  try {
    await fetchSomethingElse();
  } catch (err) {
    throw err;
  }

  try {
    let data = await fetchLastThing();
    showTheData(data);
  } catch (err) {
    throw err;
  }
};

and how it can be done with asyncresult-js

import { wrapMethod } from 'asyncresult-js';
// wrapping methods with special util
const fetchSomethingAsync = wrapMethod(fetchSomething);
const fetchSomethingElseAsync = wrapMethod(fetchSomethingElse);
const fetchLastThingAsync = wrapMethod(fetchLastThing);

// assume we are in some async context
async () => {
  let res = await fetchSomethingAsync();
  if (res.isError()) {
    throw res.err();
  }

  res = await fetchSomethingElseAsync();
  if (res.isError()) {
    throw res.err();
  }

  res = await fetchLastThingAsync();
  if (res.isError()) {
    throw res.err();
  } else {
    showTheData(res.val());
  }
};

install

npm i asyncresult-js

or

yarn add asyncresult-js

content

class AsyncResult - constructor(error, value)

every wrapped and awaited method returns instance of AsyncResult

instance methods

  • isError()
    returns true if there is error, empty string treated as error.

  • isOk()
    returns true if there is no error

  • isEmpty()
    returns true if there is no error and any value.

  • hasValue()
    returns true if there is any value. empty string treated as value.

  • err()
    returns error.

  • val()
    returns value.

  • errOrVal()
    returns error or value in such order.

  • setValue(value)
    sets given value.

  • setError(err)
    sets given error.

static methods

  • AsyncResult.success(value)
    shorthand for new AsyncResult(null, value)
  • AsyncResult.fail(erro)
    shorthand for new AsyncResult(error)

example

import { AsyncResult } from 'asyncresult-js';
let r1 = new AsyncResult(null, "foo"); // same as AsyncResult.success('foo')
console.log(r1.isError()); // false
console.log(r1.val()); // 'foo'

let r2 = new AsyncResult("foo"); // same as AsyncResult.fail('foo')
console.log(r1.isError()); // true
console.log(r1.err()); // 'foo'

util toAsyncResult(arg, AsyncResultClass)

converts given argument to a promise which will be resolved or rejected with AsyncResult instance.
If arg is instanceof Error then it will goes to the error value

arg - any, required
AsyncResultClass - optional, pass if you need your own extended AsyncResult

examples

import { toAsyncResult } from 'asyncresult-js';

// assume we are in some async context
async () => {
  let mypromise = Promise.resolve("foo");
  let value = await mypromise; // 'foo';
  let mypromiseAsync = toAsyncResult(mypromise);
  value = await mypromiseAsync; // AsyncResult { value: 'foo' }
};

util wrapMethod(method, options)

returns new method which always return promise which will resolve with AsyncResult

method - function, required.
options - object, optional.

options

context - binds returned new function to this context.
AsyncResult - AsyncResult constructor, provide it if you need extended version of AsyncResult

examples

import { wrapMethod } from 'asyncresult-js';

const myMethod = function() {
  return "foo";
};

const myAsyncMethod = wrapMethod(myMethod);

let result = myAsyncMethod(); // returns promise;

// assume we are in some async context
async () => {
  result = await myAsyncMethod(); // returns AsyncResult with value 'foo';
};

util addAsync(context, methodNames, options)

adds async version of exist methods, founded by given names

example:

import { addAsync } from 'asyncresult-js'

const Something = {
	foo() { ... },
	bar() { ... },
	baz() { ... },
}

addAsync(Something, ['foo', 'bar']); // adds to Something fooAsync, and barAsync methods

addAsync(Something, 'baz'); // adds to Something bazAsync method

in general its a sugar for this operation

const Something = {
	foo() { ... },
}
Something.fooAsync = wrapMethod(Something.foo, { context: Something })

Also, its possible to extend prototype of some class
You can do this in two ways

  1. patch prototype
addAsync(SomeClass.prototype, [...], { context: null });

Note, that there should be null context specified in options.

  1. pass class itself as argument
addAsync(SomeClass, [...]);

actually its just a suggar for case 1.

Warning: Note, that both modifies prototype so you should know what are you doing.

In case you want to add class static methods you have to use option static

// assume that SomeClass.staticMethod is a function.
addAsync(SomeClass, 'staticMethod', { static: true });
await SomeClass.staticMethodAsync();

arguments

context - object, required. Context for methods lookup and for adding asynced methods.
methodNames - string | array of strings, required. Array of methods names or single method name to be wraped.
options - object, optional.

options

context - forced context, will be used internally in wrapMethod as context.
AsyncResult - AsyncResult class, in case you need extend version of AsyncResult.

config

keeps default AsyncResult class, you can replace it with your own.
Used by default if there is no AsyncResult option provided.

example

import { config, wrapMethod } from 'asyncresult-js';
config.AsyncResult = MyOwnAsyncResult;

const test = () => true;
const testAsync = wrapMethod(test);

// assume we are in some async context
async () => {
  const result = await testAsync();
  console.log(result instanceof MyOwnAsyncResult); // true
};