javascript - JS lambda default return value - Stack Overflow

I am a bit confused with the default return value of lambdas in node.js. I found this link "Arrow

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
Add a ment  | 

2 Answers 2

Reset to default 4

The arrow function in your example does not return anything. However it writes to the response 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744368031a4570805.html

相关推荐

  • javascript - JS lambda default return value - Stack Overflow

    I am a bit confused with the default return value of lambdas in node.js. I found this link "Arrow

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信