An open API service indexing awesome lists of open source software.

https://github.com/jeongbaebang/async-wave

비동기 콜백을 순차 실행 및 관리를 간소화하는 JavaScript 비동기 라이브러리
https://github.com/jeongbaebang/async-wave

async axios promise promise-chain typescript-definitions

Last synced: 4 months ago
JSON representation

비동기 콜백을 순차 실행 및 관리를 간소화하는 JavaScript 비동기 라이브러리

Awesome Lists containing this project

README

          



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.

- [🇰🇷 한국어](./README.md)

## Table of Contents

- [Installing](#installing)
- [Package manager](#package-manager)
- [CDN](#cdn)
- [Usage](#usage)

## Installing

### Package manager

Using npm:

```bash
$ npm install async-wave
```

Using yarn:

```bash
$ yarn add async-wave
```

### CDN

Using unpkg CDN:

```html

```

# Usage

### Before

```ts
// 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

```typescript
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([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.