Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exporting a default async function is not defined in current file

The following works perfectly:

export default function x () {
  return 'hello world'
}

export function y () {
  return x()
}

console.log(y())

However this does not work:

export default async function x () {
  return 'hello world'
}

export function y () {
  return x()
    .then(console.log)
}

y()

When the default function is async for some reason x is not defined.

like image 753
ThomasReggi Avatar asked Dec 17 '16 22:12

ThomasReggi


People also ask

Can we export async function?

Can we export async function? This is not possible. Since the value is retrieved asynchronously, all modules that consume the value must wait for the asynchronous action to complete first – this will require exporting a Promise that resolves to the value you want.

How to define an async function in JavaScript?

An async function is a function declared with the async keyword, and the await keyword is permitted within it. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.

What is default export in JavaScript?

The export statement is used when creating JavaScript modules to export objects, functions, variables from the module so they can be used by other programs with the help of the import statements.

What is difference between export and export default?

Exports without a default tag are Named exports. Exports with the default tag are Default exports. Using one over the other can have effects on your code readability, file structure, and component organization. Named and Default exports are not React-centric ideas.


1 Answers

Looks like this is a known issue inside babel project: https://github.com/babel/babel/issues/3786

like image 125
nvartolomei Avatar answered Sep 28 '22 18:09

nvartolomei