Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Save some data
- */
- const persistData = data => {
- // persist the data
- };
- /**
- * Fetch some data, persist it and resolve a promise by given resolve function.
- *
- * @param {function} resolve - Some resolve from some Promise
- * @return {Promise} - A promise from the fetch api
- */
- const fetchAndPersistAndResolve = resolve =>
- fetch('url')
- .then(data => {
- persistData(data.json());
- resolve();
- });
- /**
- * This function is passed below to the promise creator, so it becomes the promise's guard
- *
- * @return {boolean}
- */
- const checkSomething = () => true;
- /**
- * Create a promise which uses a given guard and a given external resolver
- *
- * @param {function} guard - A guard that returns a boolean, false if the promise needs to be rejected
- * @param {function} externalResolver - An external function, performs other tasks before resolving the promise
- */
- const createPromiseWithGuardAndExternalResolver = (guard, externalResolver) =>
- new Promise((resolve, reject) => {
- if (!guard()) {
- reject();
- // TODO: not sure, if this return statement is necessary
- return;
- }
- externalResolver(resolve)
- .catch(reject);
- });
- const p = createPromiseWithGuardAndExternalResolver(checkSomething, fetchAndPersistAndResolve);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement