Here is some synchronous code:
let barsClient = [{
id: 1,
name: "The Shindig",
goingTonight: []
},
{
id: 2,
name: "Steakapolis",
goingTonight: []
},
{
id: 3,
name: "Booty Town",
goingTonight: []
}
];
let barsDB = [{
id: 2,
name: "Steakapolis",
goingTonight: ["Joe", "Jim", "Joey"]
}];
function findById(bar) {
return barsDB.find((dbBar) => {
return dbBar.id === bar.id;
});
}
// Check to see if bar is present in barsDB
for (let bar of barsClient) {
let result = findById(bar);
// If bar is not in barsDB, add it
if (!result) {
barsDB.push(bar);
// If it is present, update its goingTonight property
} else {
bar.goingTonight = result.goingTonight;
}
}
// Both barsClient and barsDB should contain the same data, albeit not necessarily in the same order
console.log("barsClient:", barsClient);
console.log("barsDB:", barsDB);
Now, imagine that the findById
function was asynchronous? It took me about six hours of Googling, reading and fiddling to finally figure this one out and I'm pretty happy with myself. I'm just wondering if my solution is in accordance with best practices. Here is the code to review:
let barsClient = [{
id: 1,
name: "The Shindig",
goingTonight: []
},
{
id: 2,
name: "Steakapolis",
goingTonight: []
},
{
id: 3,
name: "Booty Town",
goingTonight: []
}
];
let barsDB = [{
id: 2,
name: "Steakapolis",
goingTonight: ["Joe", "Jim", "Joey"]
}];
function findById(bar) {
return barsDB.find((dbBar) => {
return dbBar.id === bar.id;
});
}
function findByIdAsync(bar) {
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(findById(bar));
}, 3000);
});
return promise;
}
Promise.all(barsClient.map((bar, i) => {
return findByIdAsync(bar)
.then(function(result) {
if (!result) {
barsDB.push(bar);
} else {
bar.goingTonight = result.goingTonight;
}
});
}))
.then(() => {
console.log("barsClient:", barsClient);
console.log("barsDB", barsDB);
});