@prodo-ai/js-async
JS utilities for async/await
Last updated 2 years ago by tdawes .
UNLICENSED · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @prodo-ai/js-async 
SYNC missed versions from official npm registry.

JS ASync

Utility library for handling async/await.

Usage

denodeify

Turns a function that accepts a node-style callback into one that returns a Promise:

import {denodeify} from "@prodo-ai/js-async";

const nodeStyleFunction = (arg1, arg2, callback) => {
  if (arg1 == null) {
    callback(new Error("Must specify `arg1`."));
  }
  callback(null, arg1 + arg2);
}

const asyncFunction = denodeify(nodeStyleFunction);

asyncFunction(1, 2)
  .then(console.log)
  .catch(console.error);

fromCallback

The same as denodeify, but without error handling. Probably don't use this.

keepTrying

Keeps attempting a behaviour until it succeeds:

import {keepTrying} from "@prodo-ai/js-async";

const result = await keepTrying({
  behaviour: () => service.getResult()
});

You can also pass additional parameters for greater control:

  • until - a predicate that can be used to determine success. By default, this predicate just checks if the result is non-null.
  • ignoreErrors - if false, gives up trying after an error is thrown.
  • timeout - a timeout, after which an Error is thrown.
  • inbetweenAttempts - a callback that is executed between each attempt.

waitUntil

Waits until a predicate passes:

import {waitUntil} from "@prodo-ai/js-async";
import {duration, SECOND} from "@prodo-ai/js-timing";

waitUntil({
  condition: async () => (await service.getStatus()) === "OK",
  pauseTime: duration(1, SECOND),
}).then(service.getResult);

You can also pass additional parameters for greater control:

  • timeout - a timeout, after which an Error is thrown.

Information

Owner: Prodo Tech Ltd

Maintainer: tdawes

License: UNLICENSED (for now)

Current Tags

  • 1.0.1                                ...           latest (2 years ago)

2 Versions

  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 2
Dependencies (2)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |