Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----
- function rejectSometimes() {
- return new Promise((resolve, reject) => {
- setTimeout(function() {
- const judge = Math.random() >= 0.5;
- if (judge) {
- resolve(Math.random() * 100)
- } else {
- reject('dao error');
- }
- }, 1000);
- });
- }
- //-----
- async function hogeService() {
- const result1 = await rejectSometimes().catch(handleError('hoge fuga!!!!!'));
- const result2 = await rejectSometimes().catch(handleError('foo bar*****'));
- console.log('--------------------');
- console.log(result1);
- console.log(result2);
- console.log('--------------------');
- return result1 + result2;
- }
- function handleError(message) { // curry
- return function(error) {
- // エラー時にjkcmnLog.errorLogを呼び出すとか、そういうのを書けばいい
- console.log(message);
- console.log(error);
- throw error;
- }
- }
- //-----
- hogeService()
- .then(result => {
- console.log('ok!!!');
- console.log(result);
- })
- .catch((err) => {
- console.log('catch!!!');
- console.log(err);
- });
- // $ node async_sample.js
- // hoge fuga!!!!!
- // dao error
- // catch!!!
- // dao error
- // $ node async_sample.js
- // foo bar*****
- // dao error
- // catch!!!
- // dao error
- // $ node async_sample.js
- // --------------------
- // 4.877336342569638
- // 64.98860977712235
- // --------------------
- // ok!!!
- // 69.86594611969198
Add Comment
Please, Sign In to add comment