DTP Base provides a scalable and secure Node.js application development harness ready for production service.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

39 lines
872 B

// site-async.js
// Copyright (C) 2022,2024 DTP Technologies, LLC
// All Rights Reserved
'use strict';
export class SiteAsync {
static each (sourceItems, callback, concurrent = 1) {
if (!Array.isArray(sourceItems)) {
throw new Error('each requires an array of objects to be processed');
}
if (sourceItems.length === 0) {
return Promise.resolve();
}
var items = sourceItems.slice();
var running = 0;
return new Promise((resolve, reject) => {
function next ( ) {
let item = items.shift();
if (!item) {
return;
}
++running;
callback(item).then(next).catch(reject).finally(( ) => {
if (--running === 0) {
resolve();
}
});
}
while (concurrent && items.length) {
next();
--concurrent;
}
});
}
}