Reputation: 5260
I have a method that passed an array of object call on each object an asynchronous method:
function method(objs){
for(obj in objs){
obj.asyncMethod()
}
}
I have this idea that every function should return a value, but in this case the value should be based on the result of the async method so it's quite impossible.
is it a bad practic not to return a value (especially in library)?
if yes, there are solutions?
Upvotes: 1
Views: 490
Reputation: 5197
A function should only return a value if it needs to return a value. There is absolutely nothing wrong with writing a function that does not return anything.
To answer your other question: no, there is no solution to this since inherently the method
function will return before the asynchronous methods execute.
What would you even return? The code will go through n iterations in that for
loop so how would you know which one to even return?
Upvotes: 2
Reputation: 71810
It is not bad practice to not return a value for async operations, it is expected. Accept a callback function as argument. There are quite a few built in and external libraries in node.js that have methods which accept callbacks as arguments.
Upvotes: 2