1.5.2 β€’ Published 2 years ago

promise-vigilant v1.5.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Table of Contents

Installing

Package manager

Using npm:

$ npm install promise-vigilant

Using yarn:

$ yarn add promise-vigilant

CDN

Using unpkg CDN:

<script src="https://unpkg.com/promise-vigilant@1.5.0/dist/index.js"></script>

Usage

import { goAsync } from 'promise-vigilant';

// Example 1: Basic Usage
goAsync<number, number>(10, [async (num) => num + 5, (num) => num + 20], {
  onSuccess: (result) => {
    console.log(result); // 35
  },
});
goAsync<number>([10, async (num) => num + 5, (num) => num + 20], {
  onSuccess: (result) => {
    console.log(result); // 35
  },
});

// Example 2: Using Functions and Async Functions
goAsync<number, number>(() => 10, [(num) => num + 5, async (num) => num + 20], {
  onSuccess: (result) => {
    console.log(result); // 35
  },
});
goAsync<number>([() => 10, (num) => num + 5, async (num) => num + 20], {
  onSuccess: (result) => {
    console.log(result); // 35
  },
});

// Example 3: Handling Errors
goAsync(
  10,
  [
    () => {
      throw new Error('new Error!');
    },
    (num) => num + 20,
  ],
  {
    onError: (error) => {
      console.log(error.message); // new Error!
    },
  }
);
goAsync(
  [
    10,
    () => {
      throw new Error('new Error!');
    },
    (num) => num + 20,
  ],
  {
    onError: (error) => {
      console.log(error.message); // new Error!
    },
  }
);

// Example 4: Propagating Errors to External Context
(async function () {
  try {
    await goAsync(10, [
      () => {
        throw new Error('new Error2!');
      },
      (num) => num + 20,
    ]);
  } catch (error: any) {
    console.log(error.message); // new Error2!
  }
})();

// Example 5: Using with External API and Options
const placeId = '12345';
const requestPlaceDetailResultAPI = async (placeId: string) => {
  // Request place detail results from an API
  return fetch(`https://api.example.com/places/${placeId}`)
    .then((response) => response.json())
    .catch((error) => {
      throw new Error(`Failed to fetch place details: ${error.message}`);
    });
};
const createAddress = async (placeDetails: any) => {
  // Process place details and create an address
  return `${placeDetails.street}, ${placeDetails.city}, ${placeDetails.country}`;
};
const mapErrorHandler = (location: string, errorType: string) => {
  // Handle and map errors
  console.log(`Error occurred at location: ${location}, Type: ${errorType}`);
};

goAsync<string>([placeId, requestPlaceDetailResultAPI, createAddress], {
  onError: () => {
    return mapErrorHandler(placeId, ErrorType.network);
  },
  onSuccess: (data) => {
    cache.set(data.place_id, data);
  },
});

Parameters

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

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

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

Return Value

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

Example

With promise-vigilant

import { goAsync } from 'promise-vigilant';

// Example 1: Using startVal, callbacks, and option
const placeId = '12345';
const getPlaceDetailResult = async (placeId: string) => {
  // Fetch place details from an API
  return fetch(`https://api.example.com/places/${placeId}`)
    .then((response) => response.json())
    .catch((error) => {
      throw new Error(`Failed to fetch place details: ${error.message}`);
    });
};
const createAddress = async (placeDetails: any) => {
  // Process place details and create an address
  return `${placeDetails.street}, ${placeDetails.city}, ${placeDetails.country}`;
};
const mapErrorHandler = (location: string, errorType: string) => {
  // Handle and map errors
  console.log(`Error occurred at location: ${location}, Type: ${errorType}`);
};

goAsync<string, string>(placeId, [getPlaceDetailResult, createAddress], {
  onError: () => {
    return mapErrorHandler(placeId, 'network');
  },
  onSuccess: (data) => {
    console.log('Place details:', data);
    // Store place details in a cache
  },
})
  .then((result) => {
    console.log('Final result:', result);
  })
  .catch((error) => {
    console.error('Error:', error);
  });

// Example 2: Using only callbacks and option
const fetchData = async () => {
  // Fetch data from an API
  return fetch('https://api.example.com/data')
    .then((response) => response.json())
    .catch((error) => {
      throw new Error(`Failed to fetch data: ${error.message}`);
    });
};
const processData = async (data: any) => {
  // Process data
  return data.map((item: any) => item.name);
};
const handleSuccess = (result: string[]) => {
  console.log('Processed data:', result);
  // Do something with the processed data
};
const handleError = (error: Error) => {
  console.error('Error occurred:', error);
  // Handle the error
};

goAsync<string[]>([fetchData, processData], {
  onError: handleError,
  onSuccess: handleSuccess,
})
  .then((result) => {
    console.log('Final result:', result);
  })
  .catch((error) => {
    console.error('Error:', error);
  });

License

MIT

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.4.5

2 years ago

1.2.7

2 years ago

1.4.4

2 years ago

1.2.6

2 years ago

1.5.2

2 years ago

1.4.3

2 years ago

1.2.5

2 years ago

1.5.1

2 years ago

1.4.2

2 years ago

1.2.4

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.2.3

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.1.3

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.1.2

2 years ago

1.2.9

2 years ago

1.2.10

2 years ago

1.2.11

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago