Received a very JavaScript-oriented question, probably prepared by the interviewer. Didn’t compile in the end, likely failed. Interviewing for Uber Freight team. Need help preparing! Need to come up with something, can’t see anything.
Write an async maplimit.
It means that only a limited number of tasks can run at the same time. The task id is given, the second param is the CB after each task is completed, and the third param is called after all tasks are completed. To print out the results in order (note that it is the order of inputs),
function getNameById(id, callback) {
// simulating async request
const randomRequestTime = Math.floor(Math.random() * 100) + 200;
setTimeout(() => {
callback(“User” + id)
}, randomRequestTime);
}
function mapLimit(inputs, limit, iterateeFn, callback) {
// implement here
}
mapLimit([1,2,3,4,5], 2, getNameById, (allResults) => {
console.log(allResults) // [“User1”, “User2”, “User3”, “User4”, “User5”]
})