From d58cd652d83c23d45a724809506f8395c3775886 Mon Sep 17 00:00:00 2001 From: Benaiah Mischenko Date: Thu, 6 Apr 2017 16:51:01 -0700 Subject: [PATCH] Cleanup `resolvePromiseProperties` There were a couple issues with the original version of `resolvePromiseProperties`: - The wrapper promise was unnecessary, since we can just return the `Promise.all`. Fixing this allows us to remove a wrapping function, reduce indentation, remove the `resolve` and `reject` calls, and remove the now unnecessary `.catch` line. - There was inadvertent mutation in the `Object.assign` call - the first parameter to `Object.assign` is mutated, so to call it without mutating existing objects the first parameter should be a literal `{}`. This is now fixed. --- src/lib/promiseHelper.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/lib/promiseHelper.js b/src/lib/promiseHelper.js index 9d44c2d9..0d16bd5e 100644 --- a/src/lib/promiseHelper.js +++ b/src/lib/promiseHelper.js @@ -4,20 +4,17 @@ export const filterPromises = (arr, filter) => Promise.all(arr.map(entry => filter(entry))) .then(bits => arr.filter(entry => bits.shift())); -export const resolvePromiseProperties = obj => - (new Promise((resolve, reject) => { - // Get the keys which represent promises - const promiseKeys = Object.keys(obj).filter( - key => obj[key] instanceof Promise); +export const resolvePromiseProperties = (obj) => { + // Get the keys which represent promises + const promiseKeys = Object.keys(obj).filter( + key => typeof obj[key].then === "function"); - const promises = promiseKeys.map(key => obj[key]); + const promises = promiseKeys.map(key => obj[key]); - // Resolve all promises - Promise.all(promises) - .then(resolvedPromises => resolve( - // Return a copy of obj with promises overwritten by their - // resolved values - Object.assign(obj, zipObject(promiseKeys, resolvedPromises)))) - // Pass errors to outer promise chain - .catch(err => reject(err)); - })); + // Resolve all promises + return Promise.all(promises) + .then(resolvedPromises => + // Return a copy of obj with promises overwritten by their + // resolved values + Object.assign({}, obj, zipObject(promiseKeys, resolvedPromises))); +};