I'm working on inserting an array of items into an SQL server using promises for a sequential execution. To achieve this, I've come up with the following method:
const ForeachPromise = function (array, func) {
let promise = func(array[0]);
for (let i = 1; i < array.length; i++) {
promise = promise.then(() => func(array[i]));
}
return promise;
}
The concept is that when func is called, it will return a promise which is then chained to the previous promise.
...
return ForeachPromise(type.subprocessen, function(subproces) {
return newSubproces(subproces, typeId, dienstId, createData, s + 1);
});
I haven't tested this code yet, but I believe it should work as intended. However, my main concern is whether I am correctly using promises. Promises can be tricky and prone to misunderstanding, so I want to ensure that I haven't made any major mistakes.