B
B
beginer1232017-10-22 09:53:38
JavaScript
beginer123, 2017-10-22 09:53:38

In case of an error, is it necessary for the function to return something?

Suppose
there is a function, inside something that returns an answer, BUT it may not return, for example, null or an exception error, etc.
Something like this

getData(){
  response = какое то получение данных();
.....
};

That is, response can either contain a response, or null, or incorrect data, or an error, etc.
Question:
How do senior developers do
1) Always make a return response at the end of the function, and then process it where the function is called
2) Or do they return only if the answer is correct?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Popov, 2017-10-22
@be_a_dancer

Usually languages ​​have such a thing as exceptions. In case of an error, the execution of the function is interrupted, an exception is thrown. Otherwise, the function returns the result of the work. Sometimes you can handle an exception within the scope of a function. Sometimes you can handle it in the place where this function is called. Everything depends on the situation.

V
Vasily Mazhekin, 2017-10-22
@mazhekin

1) (simpler, more ancient, callbacks) bare javascript, and the worst way (with large nesting, get a callback hell) pass two success and error functions as parameters to the getData function and handle their calls outside.
var success = function(data) {...}
var error = function(errorData) {...}
getData(success, error)
2) (more complicated, more modern, promises) is it possible to use promises? Then always return a promise from the function. And then process success/failure outside the function along the chain.
var success = function(data) {...}
var error = function(errorData) {...}
getData().then(success, error)
3) (even more complicated, completely modern, flows) it is possible to use the rxjs library (it also works in js). Return an Observable<your type> and, outside of the function, process the result with various rxjs functions (map, do, combineLatext, zip, forkJoin, catch, merge ... and many more).
var success = function(data) {...}
var error = function(errorData) {...}
getData().map(success).catch(error) The point
is that your functions that receive data always return the info by default with data or error. And they know nothing about the external environment.
This is not what seniors do, but popular javascript frameworks

Z
Zakharov Alexander, 2017-10-22
@AlexZaharow

Dear Don. There is also a third option. Pass an object to the function and assign one of its properties that the function actually received the final result. And without setting this property, any answer is considered invalid.
Everything depends on the task.

G
Griboks, 2017-10-22
@Griboks

Well, the most repulsed = effective option. Crash js so that the entire call-stack stops and does nothing else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question