admin管理员组文章数量:1406178
I am a bit confused with the default return value of lambdas in node.js. I found this link "Arrow Functions" :
Arrow functions can have either a "concise body" or the usual "block body".
In a concise body, only an expression is specified, which bees the implicit return value. In a block body, you must use an explicit return statement.
var func = x => x * x;
// concise body syntax, implied "return"
var func = (x, y) => { return x + y; };
// with block body, explicit "return" needed
Which makes it pretty clear, but then I came upon this piece of Express code which I tested that returns the last statement by default without requiring to use "return":
const express = require('express');
const app = express();
app.use('/api/posts', (req, res, next) => {
const posts = [
{
id: 'sdfj234j654j',
title: 'First server-side post',
content: 'This is ming from the server'
},
{
id: '9054jk4ju59u90o',
title: 'Second server-side post',
content: 'This is ming from the server!'
}
];
// this is returned by default and does not require "return "
res.status(200).json({
message: 'Posts fetched succesfully!',
posts: posts
});
});
So which one is it do I need to use the return statement on lambdas when I use the block quotes to define them or not ? or is there an exception case which I am unaware of?
I am a bit confused with the default return value of lambdas in node.js. I found this link "Arrow Functions" :
Arrow functions can have either a "concise body" or the usual "block body".
In a concise body, only an expression is specified, which bees the implicit return value. In a block body, you must use an explicit return statement.
var func = x => x * x;
// concise body syntax, implied "return"
var func = (x, y) => { return x + y; };
// with block body, explicit "return" needed
Which makes it pretty clear, but then I came upon this piece of Express code which I tested that returns the last statement by default without requiring to use "return":
const express = require('express');
const app = express();
app.use('/api/posts', (req, res, next) => {
const posts = [
{
id: 'sdfj234j654j',
title: 'First server-side post',
content: 'This is ming from the server'
},
{
id: '9054jk4ju59u90o',
title: 'Second server-side post',
content: 'This is ming from the server!'
}
];
// this is returned by default and does not require "return "
res.status(200).json({
message: 'Posts fetched succesfully!',
posts: posts
});
});
So which one is it do I need to use the return statement on lambdas when I use the block quotes to define them or not ? or is there an exception case which I am unaware of?
Share Improve this question asked Nov 11, 2018 at 14:46 AzkronAzkron 411 silver badge6 bronze badges 2- Not all JS functions return a value. But once you do want to return a value, then the difference between expression-bodied arrow functions to block-bodied ones is more obvious – haim770 Commented Nov 11, 2018 at 14:49
- Returned where? – Jonathan Commented Nov 11, 2018 at 14:50
2 Answers
Reset to default 4The arrow function in your example does not return anything. However it writes to the res
ponse by calling .json({ /*...*/})
, therefore it kind of "returns" the json to the client.
A simplified example:
setTimeout(() => {
console.log("test");
}, 1);
The above code outputs something to the console although nothing gets returned from the arrow function.
I believe whatever you have marked as 'returned by default' is not actually getting returned. The function returns undefined
, as any function without a return statement does. In app.use
, what happens is, whenever that particular route is encountered, the function gets called, that's all. res.status
writes to the network only and is not returning any value.
本文标签: javascriptJS lambda default return valueStack Overflow
版权声明:本文标题:javascript - JS lambda default return value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744368030a2602879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论