1.5.2 β’ Published 9 months ago
promise-vigilant v1.5.2
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
1.2.0
11 months ago
1.1.1
11 months ago
1.1.0
11 months ago
1.4.5
10 months ago
1.2.7
11 months ago
1.4.4
10 months ago
1.2.6
11 months ago
1.5.2
9 months ago
1.4.3
10 months ago
1.2.5
11 months ago
1.5.1
9 months ago
1.4.2
10 months ago
1.2.4
11 months ago
1.5.0
9 months ago
1.4.1
10 months ago
1.2.3
11 months ago
1.4.0
10 months ago
1.3.1
11 months ago
1.1.3
11 months ago
1.3.0
11 months ago
1.2.1
11 months ago
1.1.2
11 months ago
1.2.9
11 months ago
1.2.10
11 months ago
1.2.11
11 months ago
1.0.3
12 months ago
1.0.2
12 months ago
1.0.1
12 months ago
1.0.0
12 months ago