mirror of
https://github.com/strapi/strapi.git
synced 2025-07-27 10:56:36 +00:00
45 lines
876 B
JavaScript
45 lines
876 B
JavaScript
'use strict';
|
|
|
|
const pMap = require('p-map');
|
|
const { curry } = require('lodash/fp');
|
|
|
|
function pipeAsync(...methods) {
|
|
return async (data) => {
|
|
let res = data;
|
|
|
|
for (const method of methods) {
|
|
res = await method(res);
|
|
}
|
|
|
|
return res;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @type { import('./async').MapAsync }
|
|
*/
|
|
const mapAsync = curry(pMap);
|
|
|
|
/**
|
|
* Because of how mysql works, making parallel requests can cause deadlocks.
|
|
* This function will run the requests sequentially if mysql is used. Else,
|
|
* it will encapsulate them in a Promise.all.
|
|
*
|
|
* @type { import('./async').MapAsync }
|
|
*/
|
|
const mapAsyncDialects = async (array, func) => {
|
|
switch (strapi.db.dialect.client) {
|
|
case 'mysql': {
|
|
return mapAsync(array, func);
|
|
}
|
|
default:
|
|
return Promise.all(array.map(func));
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
mapAsync,
|
|
mapAsyncDialects,
|
|
pipeAsync,
|
|
};
|