ES6 async exports

ok, here is an edge case for you experts…

I have a file in a code base that used to export a string, say a token. This was, of course a bad thing, so the value of a string was stored in an API service, and I was tasked with converting the file used to export it.

export const myString ="someword";


import apiThingie from 'whatever';
export const myString = apiThingie('service/myString') // returns a Promise

The problem is…the rest of the codebase importing myString s expecting a string, so…since exports must be done at the root level, we can’t export the result from within .then(); we must export the promise. But since the rest of the code expects a string exporting a promise (as all google results suggest) will cascade into a rewrite of a considerable amount of code.

BUT WAIT THERE IS MORE… some of the code that imports myString simply manipulates the string the EXPORTS the results of that manipulation. That means we are painted into a corner, since importing a promise means, as far as I know, I have to use it asynchronously in a function… which means I can’t export result from within that function, but I need to use an function to modify it.


import {myString} from 'file';
export const newString =  someFooCall(arg, arg, myString + " oh yea?");


import {myString} from 'file';
export myString.then( res => someFooCall(arg, arg, res + " oh yea?";) 
/*???? someFooCall is a utility that expects a string as the 3rd arg and has other function which return strings  call it... so this is beginning to cascade*/

I guess am asking, how do I extract blocking code ( a string) from async code ( a promise) or can anyone think of a way to handle this that won’t incur an entire codebase refactor?

This should be possible with Top level await

import myString from 'file';
export await myString;
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.