A little bit longer but more understandable solution , also it aways give ordered output according on the input list const chop = (input, limit) => { let res = [] let i = 0 let k = 0 while (i < input.length) { let temp = [] while (k < limit) { if (i == input.length) { break } temp.push(input[i]) k++ i++ } res.push(temp) k = 0 } return res } function getNamebyId(id, callback) { const rRt = Math.floor(Math.random() * 100) + 200; setTimeout(() => { callback('User' + id) }, rRt) } const performOp = async (choppedInput, iterateeFn) => { try { let resultList = []; for (let i = 0; i < choppedInput.length; i++) { for (let j = 0; j < choppedInput[i].length; j++) { const result = await new Promise((resolve) => { iterateeFn(choppedInput[i][j], resolve); }); resultList.push(result); } } return resultList; } catch (error) { throw error; } } async function mapLimit(inputs, limit, iterateeFn, callback) { const choppedInput = chop(inputs, limit) const res = await performOp(choppedInput, iterateeFn) callback(res) } mapLimit([5,3,2,4,1], 2, getNamebyId, (allResults) => { console.log("output", allResults) })
Greetings, I have seen you async sequence video where you finally mentioned sequence can be done in async await way also, if we could do the same for this problem it will avoid the complexity of reduce right, I tried but I'm lacking when again durning the async parallel inside that await part, do you have another video for during this with async await way please?
Why are you batching here ? Max limit of operations is 2 right. So if you are batching, you are waiting both 1 & 2 to be completed to run 3. 3 can be run right away when either 1 or 2 is completed right ?
Ideal it should be but if you read the implementation details here -> caolan.github.io/async/v3/docs.html#mapLimit it say next iteration should take place when previous is done. This is a something that can be clarified with the interviewer.
I think @justlifethings4225 is right, this is a little different question that what you have solved. The question clearly mentions max number of operations happening at the same time is the limit. Once one of your promise resolves you have to bring other from the array into process. Not batching.
A little bit longer but more understandable solution , also it aways give ordered output according on the input list
const chop = (input, limit) => {
let res = []
let i = 0
let k = 0
while (i < input.length) {
let temp = []
while (k < limit) {
if (i == input.length) {
break
}
temp.push(input[i])
k++
i++
}
res.push(temp)
k = 0
}
return res
}
function getNamebyId(id, callback) {
const rRt = Math.floor(Math.random() * 100) + 200;
setTimeout(() => {
callback('User' + id)
}, rRt)
}
const performOp = async (choppedInput, iterateeFn) => {
try {
let resultList = [];
for (let i = 0; i < choppedInput.length; i++) {
for (let j = 0; j < choppedInput[i].length; j++) {
const result = await new Promise((resolve) => {
iterateeFn(choppedInput[i][j], resolve);
});
resultList.push(result);
}
}
return resultList;
} catch (error) {
throw error;
}
}
async function mapLimit(inputs, limit, iterateeFn, callback) {
const choppedInput = chop(inputs, limit)
const res = await performOp(choppedInput, iterateeFn)
callback(res)
}
mapLimit([5,3,2,4,1], 2, getNamebyId, (allResults) => {
console.log("output", allResults)
})
Greetings, I have seen you async sequence video where you finally mentioned sequence can be done in async await way also, if we could do the same for this problem it will avoid the complexity of reduce right, I tried but I'm lacking when again durning the async parallel inside that await part, do you have another video for during this with async await way please?
Figured it out, here is the async await way of yours for sequence order,
const maxLimit = async (inputs, limit, logicFn, callback) =>{
const chopped = chop(inputs, limit);
let finalResult = [];
for(let input of chopped){
try{
let res = await new Promise((resolve, reject) =>{
let temp = [];
input.forEach((item) =>{
getNumberUserId(item, (op) =>{
temp.push(op)
if(temp.length === input.length){
resolve(temp);
}
})
})
})
finalResult = [...finalResult, ...res];
} catch (e){
console.log(e)
}
}
callback(finalResult );
}
Thanks!!
Great video,
Why are you batching here ? Max limit of operations is 2 right. So if you are batching, you are waiting both 1 & 2 to be completed to run 3. 3 can be run right away when either 1 or 2 is completed right ?
Ideal it should be but if you read the implementation details here -> caolan.github.io/async/v3/docs.html#mapLimit it say next iteration should take place when previous is done.
This is a something that can be clarified with the interviewer.
@@Learnersbucket cool 👍 love your content anyways.
I think @justlifethings4225 is right, this is a little different question that what you have solved. The question clearly mentions max number of operations happening at the same time is the limit. Once one of your promise resolves you have to bring other from the array into process. Not batching.
Bhaiya do you looking for video editor?
No, I don't do youtube full time
Hi Prashant nice video✓
When you saw this question first time did you able to solve it on your own ? Or Did you take any reference ?
It took me sometime to understand the problem, but once I read about what the method does by reading in Async library, I was able to implement it.
👍👍
Would following approach be acceptable? This works correctly.
class A{
constructor(numOfProcesses, cb, limit){
this.limit = limit;
this.waitingList = [];
this.size = 0;
this.numOfProcesses = numOfProcesses;
this.responses = [];
this.cb = cb;
}
subscribe(fn){
if(this.size===this.limit) this.waitingList.push(fn);
else{
this.size++;
this.execute(fn);
}
}
async execute(fn){
const res = await fn();
this.responses.push(res);
if(this.waitingList.length>0){
return this.execute(this.waitingList.shift())
}else{
this.size--;
}
if(this.responses.length === this.numOfProcesses){
this.cb(this.responses);
}
}
};
async function getNameById(id){
const time = 3000
return new Promise(res => setTimeout(()=>{
return res(`User${id}`)
},time))
}
function mapLimit(inputs, limit, iterateeFn, cb){
const obj = new A(inputs.length,cb,limit);
inputs.forEach((input)=>{
obj.subscribe(iterateeFn.bind(this, input))
})
}
mapLimit([1,2,3,4,5],1,getNameById,(answer)=>{
console.log(answer)
})