Skip to content

Latest commit

 

History

History
93 lines (70 loc) · 2.91 KB

README-US.md

File metadata and controls

93 lines (70 loc) · 2.91 KB

async-wave logo

`async-wave` is a safely executed asynchronous function that sequentially executes a chain of callback functions and returns the result. It safely transforms any value into a Promise and passes it as an argument to the asynchronous function. This allows for easy management of asynchronous operations and returning of results. Additionally, `asyncWave` provides intuitive implementation of error handling, success handling, and other logic through callback functions. This enables developers to write safe and efficient asynchronous code.

Table of Contents

Installing

Package manager

Using npm:

$ npm install async-wave

Using yarn:

$ yarn add async-wave

CDN

Using unpkg CDN:

<script src="https://unpkg.com/async-wave@{{VERSION}}/dist/bundle.js"></script>

Usage

Before

// Promises chaining
await setFetchLog();
startLoadingIndicator();
getGithubUser(USER_NAME)
  .then(loadJson)
  .then(showAvatar)
  .then((githubUser) => console.log(`avatar_url: ${githubUser.avatar_url}`))
  .catch((error) => console.error(error))
  .finally(endLoadingIndicator);

After

import { asyncWave } from 'async-wave';

// if a function is passed as the first argument and its return value is not a promise, an error will be thrown.
asyncWave<GithubUser>([USER_NAME, getGithubUser, loadJson], {
  onBefore: async () => {
    await setFetchLog(); // Errors inside the handler are also caught! [1]
    startLoadingIndicator();
  },
  onSuccess: async (githubUser) => {
    await showAvatar(githubUser); // Errors inside the handler are also caught! [2]
    console.log(`avatar_url: ${githubUser.avatar_url}`);
  },
  onError: (error) => {
    console.error(error);
  },
  onSettled: () => {
    endLoadingIndicator();
  },
});

Parameters

  • callbacks: An array of callback functions to be executed in the then method. (Note: If a function is passed as the first argument and its return value is not a promise, an error will be thrown.)
  • option (optional): An optional object that provides the following callback functions:
    • onBefore: A function that runs before the promise starts. This function must be passed to the async function.
    • onError: A function triggered when the promise reaches a rejected state.
    • onSuccess: A function triggered when the promise reaches a resolved state. The result of the last promise is passed as an argument to this function.
    • onSettled: A function triggered when the promise reaches either a resolved or a rejected state.

Return Value

A Promise object that returns the result of the last promise in the chain.