简体   繁体   English

如何在nodejs中的另一个箭头函数中调用箭头函数

[英]How to call arrow function in another arrow function in nodejs

Hey I have three functions in my javascript file like this嘿,我的 javascript 文件中有这样的三个函数

token.js令牌.js

const function1 = async (token) => {
  .....
};

const function2 = async(token, permission) => {
    // I want to call function1 here like
    function1(token);
};

module.exports = { function1, function2 }

In function2 I want to call function it is giving me an error function1 is not a functionfunction2我想调用 function 它给了我一个错误function1 is not a function

Does anyone know how to resolve this ?有谁知道如何解决这个问题?

It should be working.它应该工作。 Check this: working functions检查这个:工作功能

 'use strict'; const function1 = async (token) => { console.log(token); }; const function2 = async(token, permission) => { // I want to call function1 here like function1(token); }; function2('apple','ball');

I think the problem is the way you importing/exporting the functions: I changed your code a little and it works:我认为问题在于您导入/导出函数的方式:我稍微更改了您的代码,它可以工作:

const function1 = async (token) => {
  console.log(token)
};

const function2 = async  (token) => {
    // I want to call function1 here like
     //console.log(token)
    function1(token);
};

export const f= {  function1, function2 }

and code that using the function:和使用该函数的代码:

import {f}  from './funcs.ts'

f.function2('ooo')

look at this plunk看看这个笨蛋

Below is the solution to use function1 from say file1.js into function2 which is in another different JS file - say file2.js以下是将file1.js function1 使用到另一个不同 JS 文件中的 function2 的解决方案 - 比如file2.js

file1.js文件1.js

const function1 = async (token) => {
      .....
};

export { function1 }

And inside而里面

file2.js文件2.js

import { function1 } from './src/utils/file1' 

{
...other stuff
function1(token);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM