*serial(tasks, opts) { opts = opts || {}; try { return yield Promise.reduce(tasks, (val, str) => { val && Object.assign(opts, { val }); return this.start(str, opts); }, opts.val || null); } catch (err) { this.emit('serial_error'); } }
Promise.reduce(paths, images.updateHash, crypto.createHash('sha1')) .then(hash => hash ? hash.digest('hex').substr(0, 12) : null)
async function main() { const keys = Object.keys(source); Promise.each(locales, async (locale) => { const language = getLanguage(locale); const output = await Promise.reduce(keys, async (result, key) => { const value = source[key]; const [translated] = await translate.translate(value, language); result[key] = capitalize(translated); return result; }, {}); fs.outputJson(`./i18n/messages/${locale}.json`, output, { spaces: 2 }); }) }
Promise.reduce(roles, (acc, role) => { return mongodb.read(roleCollection, { name: role }) .then((results) => { /* istanbul ignore next */ return results.length ? acc.concat(results[0].permissions) : acc }) }, [])
test('new Promise -> reduce', function (t) { t.plan(4) twice(function () { var trans = ins.startTransaction() resolved([1, 2, 3]).reduce(function (total, value) { return new Promise(function (resolve, reject) { setImmediate(function () { resolve(total + value) }) }) }, 36).then(function (result) { t.strictEqual(result, 42) t.strictEqual(ins.currentTransaction.id, trans.id) }) }) })
/** * Populate a field by its key in a document. * @param {PopulateField} field * @param {Document} doc * @param {Object} cache * @return {Promise<Document>} doc */ function populateField(field, doc) { var cache = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var schema = doc.getSchema(); var type = schema.resolveFieldByKey(field.path); var keyPaths = schema.resolveFieldPath(doc, field.path); return Promise.reduce(keyPaths.toArray(), function (newDoc, keyPath) { return populateKeyPath(field, keyPath, newDoc, type, cache); }, doc); }
/** * Populate a field by its key in a document. * @param {PopulateField} field * @param {Document} doc * @param {Object} cache * @return {Promise<Document>} doc */ function populateField(field, doc, cache = {}) { const schema = doc.getSchema(); const type = schema.resolveFieldByKey(field.path); const keyPaths = schema.resolveFieldPath(doc, field.path); return Promise.reduce(keyPaths.toArray(), (newDoc, keyPath) => { return populateKeyPath(field, keyPath, newDoc, type, cache); }, doc); }
readdirAsync(dir).map((() => { var _ref = (0, _bluebird.coroutine)(function* (file) { const path = (0, _path.join)(dir, file); return (yield statAsync(path)).isDirectory() ? readDirAsync(path) : path; }); return function (_x) { return _ref.apply(this, arguments); }; })()).reduce((a, b) => a.concat(b), [])
function getContentTypes(spaces) { return Promise.reduce(_.keys(spaces), (contentTypes, name) => { const space = spaces[name] return space.getContentTypes() // FIXME: this will break with pagination! .then(({items}) => _.set(name, items, contentTypes)) }, {}) }
/** * Populate a field in a list of documents. * @param {PopulateField} field * @param {List<Document>} docs * @param {Object} cache * @return {Promise<List<Document>>} */ function populateFieldForDocs(field, docs) { var cache = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; return Promise.reduce(docs, function (accu, doc) { return populateField(field, doc, cache).then(function (newDoc) { return accu.concat([newDoc]); }); }, []).then(List); }
readdirAsync(dir).map(async (file) => { const path = join(dir, file) return (await statAsync(path)).isDirectory() ? readDirAsync(path) : path }).reduce((a, b) => a.concat(b), [])
test('Promise.reduce', function (t) { t.plan(4) twice(function () { var trans = ins.startTransaction() Promise.reduce([1, 2, 3], function (total, value) { return new Promise(function (resolve, reject) { setImmediate(function () { resolve(total + value) }) }) }, 36).then(function (result) { t.strictEqual(result, 42) t.strictEqual(ins.currentTransaction.id, trans.id) }) }) })
/** * Populate a field in a list of documents. * @param {PopulateField} field * @param {List<Document>} docs * @param {Object} cache * @return {Promise<List<Document>>} */ function populateFieldForDocs(field, docs, cache = {}) { return Promise.reduce(docs, function(accu, doc) { return populateField(field, doc, cache) .then(function(newDoc) { return accu.concat([newDoc]); }); }, []) .then(List); }
function getSpaces(spaceConfigs) { return Promise.reduce(spaceConfigs, (spaces, spaceConfig) => getSpace(spaceConfig) .then(space => _.set(spaceConfig.name, space, spaces)), {}) }
*serial(tasks, opts) { opts = opts || {}; try { return yield Promise.reduce(tasks, (val, str) => { val && Object.assign(opts, { val }); return this.start(str, opts); }, opts.val || null); } catch (err) { this.emit('serial_error'); } }