1.7.2 β€’ Published 1 month ago

async-wave v1.7.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

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/index.js"></script>

Usage

import { asyncWave } from 'async-wave';

type GithubUser = { avatar_url: string };

async function getGithubUser(name: string) {
  return await fetch(`https://api.github.com/users/${name}`);
}

async function loadJson(response: Response): Promise<GithubUser> {
  return await response.json();
}

function showAvatar(githubUser: GithubUser): Promise<GithubUser> {
  return new Promise(function (resolve) {
    const img = document.createElement('img');
    img.src = githubUser.avatar_url;
    img.className = 'promise-avatar-example';
    document.body.append(img);

    setTimeout(() => {
      img.remove();
      resolve(githubUser);
    }, 3000);
  });
}

const USER_NAME = 'jeongbaebang';

// 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);

// with asyncWave
asyncWave<GithubUser>([USER_NAME, getGithubUser, loadJson], {
  onBefore: async () => {
    await setFetchLog(); // ν•Έλ“€λŸ¬ λ‚΄λΆ€ μ—λŸ¬λ„ μΊμΉ˜λ©λ‹ˆλ‹€! [1]
    startLoadingIndicator();
  },
  onSuccess: async (githubUser) => {
    await showAvatar(githubUser); // ν•Έλ“€λŸ¬ λ‚΄λΆ€ μ—λŸ¬λ„ μΊμΉ˜λ©λ‹ˆλ‹€! [2]
    console.log(`avatar_url: ${githubUser.avatar_url}`);
  },
  onError: (error) => {
    console.error(error);
  },
  onSettled: () => {
    endLoadingIndicator();
  },
});

Parameters

  • startValue (선택 사항): ν”„λ‘œλ―ΈμŠ€λ‘œ λ³€ν™˜λ  첫 번째 κ°’μž…λ‹ˆλ‹€. λ§Œμ•½ 이 값이 ν•¨μˆ˜λ‚˜ ν”„λ‘œλ―ΈμŠ€κ°€ μ•„λ‹Œ 경우, μžλ™μœΌλ‘œ ν”„λ‘œλ―ΈμŠ€λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜λ‘œ λ³€ν™˜λ©λ‹ˆλ‹€. 첫 번째 인자둜 μ „λ‹¬λœ 값은 μ–΄λ–€ 값이든 ν”„λ‘œλ―ΈμŠ€λ‘œ κ°μ‹Έμ Έμ„œ μ „λ‹¬λ©λ‹ˆλ‹€.

μ°Έκ³ : 첫번째 μΈμˆ˜λŠ” μ–΄λ–€ 값을 μ „λ‹¬ν•˜λ“  항상 ν”„λ‘œλ―ΈμŠ€λ‘œ κ°μ‹Έμ Έμ„œ μ „λ‹¬λ©λ‹ˆλ‹€.

  • callbacks: then λ©”μ„œλ“œμ—μ„œ μ‹€ν–‰ν•  콜백 ν•¨μˆ˜λ“€μ˜ λ°°μ—΄μž…λ‹ˆλ‹€.
  • option (선택 사항): λ‹€μŒ 콜백 ν•¨μˆ˜λ“€μ„ μ œκ³΅ν•˜λŠ” 선택적인 κ°μ²΄μž…λ‹ˆλ‹€:
    • onBefore: ν”„λ‘œλ―ΈμŠ€κ°€ μ‹œμž‘λ˜κΈ° 전에 μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. ν•΄λ‹Ή ν•¨μˆ˜λŠ” async ν•¨μˆ˜λ‘œ 전달해야 ν•©λ‹ˆλ‹€.
    • onError: ν”„λ‘œλ―ΈμŠ€κ°€ κ±°λΆ€λœ μƒνƒœμ— λ„λ‹¬ν–ˆμ„ λ•Œ μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
    • onSuccess: ν”„λ‘œλ―ΈμŠ€κ°€ ν•΄κ²°λœ μƒνƒœμ— λ„λ‹¬ν–ˆμ„ λ•Œ μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. λ§ˆμ§€λ§‰ ν”„λ‘œλ―ΈμŠ€μ˜ κ²°κ³Όκ°€ 이 ν•¨μˆ˜μ˜ 인자둜 μ „λ‹¬λ©λ‹ˆλ‹€.
    • onSettled: ν”„λ‘œλ―ΈμŠ€κ°€ ν•΄κ²°λ˜κ±°λ‚˜ κ±°λΆ€λœ μƒνƒœμ— λ„λ‹¬ν–ˆμ„ λ•Œ μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.

Return Value

μ²΄μΈμ—μ„œ λ§ˆμ§€λ§‰ ν”„λ‘œλ―ΈμŠ€μ˜ κ²°κ³Όλ₯Ό λ°˜ν™˜ν•˜λŠ” Promise κ°μ²΄μž…λ‹ˆλ‹€.

Example

import { asyncWave } from 'async-wave';

// Example 1: Using startVal, callbacks, and option
asyncWave<string, string>(placeId, [getPlaceDetailResult, createAddress], {
  onError: () => {
    return mapErrorHandler(placeId, 'network');
  },
  onSuccess: (data) => {
    console.log('Place details:', data);
    // Store place details in a cache
  },
});

// Example 2: Using only callbacks and option
asyncWave<string[]>([fetchData, processData], {
  onError: handleError,
  onSuccess: handleSuccess,
});

License

MIT

1.7.2

1 month ago

1.7.1

1 month ago

1.7.0

1 month ago

1.6.0

1 month ago

1.5.5

9 months ago

1.5.4

9 months ago

1.5.3

9 months ago