Currently you do mutate theobjects asynchronously, however you do not wait for the async calls to finish thats why it looks as if the objects were never changed. Additionally you do not return anything from inside the .map
, therefore newobj
is an array of undefined
s.
sequelize.query(query)
already returns a promise, if you return that from the .map
callback, you end up with an array of promises, which you can turn into a Promise that resolves to an array using Promise.all
:
const promise = Promise.all(obj.map(frequentPosition => {
var query = `SQL query that uses attributes of obj`;
return sequelize.query(query).then(result => {
return { ...obj, newAttribute: result[0], };
});
});
Then you can use the promise as:
promise.then(newobj => {
// Do stuff with newobj
});