首页 > 解决方案 > 如何在传递函数中返回值 (Node.js)

问题描述

我正在尝试将父函数中的变量设置为 Node.js 中的导入和嵌套函数。

家长

const child = require('./child')

function init() {
  second()
  let def = abc
  console.log(def) // Expected log: 15
                   // Actual log: abc is not defined
}

function second() {
  return child.third()
}

init()

孩子

exports.third = () => {
  let abc = 15
  return abc
}

如何正确返回 的值,abc以便可以设置和登录init()

标签: node.jscallbackpromise

解决方案


只需abc = second()在你的init()街区做


推荐阅读