mailtrain/server/lib/synchronized.js
Tomas Bures 66702b5edc Fixes in reports (generating a CSV).
Added caching of generated images in mosaico handler.
Various other fixes.
2019-04-22 02:41:40 +02:00

26 lines
No EOL
687 B
JavaScript

'use strict';
// This implements a simple wrapper around an async function that prevents concurrent execution of the function from two asynchronous chains
// It enforces that the running execution has to complete first before another one is started.
function synchronized(asyncFn) {
let ensurePromise = null;
return async (...args) => {
while (ensurePromise) {
try {
await ensurePromise;
} catch (err) {
}
}
ensurePromise = asyncFn(...args);
try {
return await ensurePromise;
} finally {
ensurePromise = null;
}
}
}
module.exports = synchronized;