最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS lambda default return value - Stack Overflow

programmeradmin5浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论