diff --git a/public/src/utils.js b/public/src/utils.js index 4b4acd5cb0..dde7096914 100644 --- a/public/src/utils.js +++ b/public/src/utils.js @@ -474,6 +474,19 @@ return object && typeof object.then === 'function'; }, + promiseParallel: function (obj) { + var keys = Object.keys(obj); + return Promise.all( + keys.map(function (k) { return obj[k]; }) + ).then(function (results) { + var data = {}; + keys.forEach(function (k, i) { + data[k] = results[i]; + }); + return data; + }); + }, + isRelativeUrl: function (url) { var firstChar = String(url || '').charAt(0); return (firstChar === '.' || firstChar === '/'); diff --git a/test/utils.js b/test/utils.js index 1984176e10..ea484891d0 100644 --- a/test/utils.js +++ b/test/utils.js @@ -4,7 +4,7 @@ var assert = require('assert'); var JSDOM = require('jsdom').JSDOM; var utils = require('./../public/src/utils.js'); - +const db = require('./mocks/databasemock'); describe('Utility Methods', function () { // https://gist.github.com/robballou/9ee108758dc5e0e2d028 @@ -407,4 +407,17 @@ describe('Utility Methods', function () { done(); }, 500); }); + + it('should return object with data', async function () { + const user = require('../src/user'); + const uid1 = await user.create({ username: 'promise1' }); + const uid2 = await user.create({ username: 'promise2' }); + const result = await utils.promiseParallel({ + user1: user.getUserData(uid1), + user2: user.getUserData(uid2), + }); + assert(result.hasOwnProperty('user1') && result.hasOwnProperty('user2')); + assert.strictEqual(result.user1.uid, uid1); + assert.strictEqual(result.user2.uid, uid2); + }); });