Możliwy jest pusty return
?
module.exists = async function (key) {
if (!key) {
return;
}
Możliwy jest pusty return
?
module.exists = async function (key) {
if (!key) {
return;
}
Tak, taki kod jest prawidłowy (chociaż async
jest niepotrzbne, gdy nie korzystamy z await
).
@Xarviel: to kod nie mojego autorstwa stąd pytanie.
Czyli jeśli jest puyste return
to zwraca sam siebie tak? Dobrze rozumiem?
Zwróci CI undefined
Puste return
zwraca wartość undefined
, czyli można powiedzieć, że taka funkcja w rzeczywistości nic nie zwraca :P
Wszystkie 3 przykłady dadzą nam taki sam wynik.
function _return() {
return;
}
function returnUndefined() {
return undefined;
}
function empty() {
}
console.log(_return()); // undefined
console.log(returnUndefined()); // undefined
console.log(empty()); // undefined
uzupełniając - funkcje oznaczone async zwracają zawsze Promise
, więc jak masz async
, to puste return
zwróci obiekt Promise
:
async function foo() {
return;
}
console.log(foo()); // wyświetli obiekt Promise
Tyle, że ponieważ było samo return;
to ten konkretny obiekt Promise
i tak się rozwiąże do undefined
(jeśli użyjemy na nim metody .then
albo zrobimy na nim await
).