简体   繁体   中英

How to call arrow function in another arrow function in nodejs

Hey I have three functions in my javascript file like this

token.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 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

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

export { function1 }

And inside

file2.js

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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