feat: add utils.promiseParallel

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 0a6cfb37c2
commit 4170abfd6b

@ -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 === '/');

@ -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);
});
});

Loading…
Cancel
Save