I have a code to update or create new users in my system. It works with a forEach function, and works well. However, I want to take advantage of Node.js and execute several updates/creations at the same time. I have tried to create an array of promises, but I think I am building the promises wrong. This is the code that works fine:
import usersjson from './jsons/users.json';
if (usersjson.users.length) {
for (const user of usersjson.users) {
let getuser = getUser(url, user.Id, 'Id');
getuser.then((data) => {//Make a call to get user by id
if (data.users.length != 0) { //If user exists
let update = updateUser(url, user, data.users[0].id);//update user function
update.then((updateresponse) => {
console.log(updateresponse);
}).catch((err) => {
console.log(err);
})
} else { //if user doesn't exist
let update = newUser(url, user);//createUser(settings.fieldMap, user)
update.then((response) => {
console.log(response);
}).catch((err) => {
console.log(err);
});
}
})
};
};
Now, I want to make the "update" into promises, so that I can then use promise.all() to send several at the same time. This is what I tried:
import usersjson from './jsons/users.json';
let promises = [];
if (usersjson.users.length) {
for (const user of usersjson.users) {
if (promises.length <= 20) {
let getuser = getUser(url, user.Id, 'Id');
getuser.then((data) => {//Make a call to get user by id
if (data.users.length != 0) { //If user exists
let update = new promise ((resolve, reject) => {
updateUser(url, user, data.users[0].id).then((response) => {
resolve(response);
}).catch((err) => {
console.log(err)
});
});
promises.push(update);
} else { //if user doesn't exist
let update = new promise ((resolve, reject) => {
newUser(url, user).then((response) => {
resolve(response);
}).catch((err) => {
console.log(err);
})
});
psomises.push(update);
}
});
} else {
await promises.all(update)
.then ((result) => {
console.log(result);
promises = [];
}).catch(err){
console.log(err);
}
};
};
};
But I get an error message:
(node:22016) UnhandledPromiseRejectionWarning: ReferenceError: promise is not defined at file:///C:/xampp/htdocs/NodeAPI/server.js:70:21 at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:22016) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:22016) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
My question is, how do I make the forEach run for those 20 users in the array at the same time instead of one by one?