0

in test.js, I have some callback that resolves "a" value. then further the "a" value is exported at the end of the file

//test.js
let fun = async ()=>{
let a
    await setTimeout(()=>{a = 10}, 100)
    return a
}

let a  
fun().then(val=>{a=val})
export default a

when im trying to import a value in test1.js, im getting a as undefiner

test1.js
import a from './test.js'

console.log(a) //undefined
2
  • 1
    await setTimeout makes no sense since setTimout doesn't return a Promise Commented Sep 19, 2022 at 5:27
  • Please illustrate the REAL problem with REAL code, not a make-up example like this which nobody would ever code this way. If you're using an actual asynchronous operation that generates a result you want to export, then please show the real code for that actual asynchronous operation. We can always help much more accurately and more specifically, than when you use make-up example code in your question. For example, if the asynchronous operation you're using is not promise-based, then the answer needs to include even more information about that. Commented Sep 19, 2022 at 8:35

2 Answers 2

2

This should work

test.js

// this does what you attempted to do
// but actually works
let fun = async () => {
    let a = await new Promise(r => setTimeout(r, 100, 10));
    return a;
};

let a = await fun();
export default a;

test1.js

import a from './test.js'
console.log(a)
Sign up to request clarification or add additional context in comments.

5 Comments

Why not use the built-in version of setTimeout() that already returns a promise. If you're using top level await it's got to already be a fairly modern version of nodejs.
@jfriend00 - because I didn't know it existed!!! :p
It was added in nodejs v15, released in Oct 2020.
@jfriend00 I can see that it was experimental in 15 and "release" in 16 - to be fair, I don't read the release notes thoroughly
Just making a suggestion for how to modernize your answer and perhaps teach a few others about the promisified version of setTimeout(). v16 was released in April 2021 so in either case, it's been available for awhile. I don't scour the release notes either, but do read a "what's new" summary of each new major MTS release which I find beneficial.
2

let is lexically scoped to the nearest block, so you actually have two different variables named a that just happen to have the same name.

The solution would be to use top-level await to export a Promise, for example:

export default await Promise.resolve(10)

6 Comments

Or use the built in version of setTimeout() that returns a promise.
Good idea, though it's not supported in Node 14 LTS. I think it would be better to just use Promise.resolve() in this example, since the length of the delay doesn't really matter.
The OP likely intends for there to be some real asynchronous operation there because if there wasn't, then there's not even a need for Promise.resolve() or setTimeout() as the value can just be exported directly.
It is supported in Node 16 LTS which was originally released April 2021 (17 months ago). v18 has been out for awhile and will probably go LTS next month.
Promise.resolve() already resolves asynchronously, so I don't see a practical difference for this example. Also, Node 14 is still supported for another 7 months, so my answer supports it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.