Implements new urisToLookupSuccessAndFailedMap function -> similar to urisToLookup but distinguishes between failed and sucess fetches in the return payload
@@ -414,6 +414,56 @@ export function urisToLookupMap(
});
}
+/**
+ * Takes a single uri or an array of uris, performs the lookup function on each
+ * of them seperately, collects the results and builds an map/object
+ * with the uris as keys and the results as values.
+ * If any call to the asyncLookupFunction fails, the corresponding
+ * key-value-pair will not be contained in the success-result but rather in the failed-results.
+ * @param uris
+ * @param asyncLookupFunction
+ * @param excludeUris uris to exclude from lookup
+ * @return {*}
+ */
+export function urisToLookupSuccessAndFailedMap(
+ uris,
+ asyncLookupFunction,
+ excludeUris = []
+) {
+ //make sure we have an array and not a single uri.
+ const urisAsArray = is("Array", uris) ? uris : [uris];
+ const excludeUrisAsArray = is("Array", excludeUris)
+ ? excludeUris
+ : [excludeUris];
+
+ const urisAsArrayWithoutExcludes = urisAsArray.filter(uri => {
+ const exclude = excludeUrisAsArray.indexOf(uri) < 0;
+ if (exclude) {
+ return true;
+ } else {
+ return false;
+ }
+ });
+
+ const asyncLookups = urisAsArrayWithoutExcludes.map(uri =>
+ asyncLookupFunction(uri).catch(error => {
+ return error;
+ })
+ );
+ return Promise.all(asyncLookups).then(dataObjects => {
+ const lookupMap = { success: {}, failed: {} };
+ //make sure there's the same
+ uris.forEach((uri, i) => {
+ if (dataObjects[i] instanceof Error) {
+ lookupMap["failed"][uri] = dataObjects[i];
+ } else if (dataObjects[i]) {
+ lookupMap["success"][uri] = dataObjects[i];
+ }
+ });
+ return lookupMap;
+ });
+}
+
/**
* Maps an asynchronous function over the values of an object or
* the elements of an array. It returns a promise with the result,
