There are a couple of ways around this. I don't know your familiarity with
Node.js, so if something doesn't make sense, please ask follow up questions.
The skinny of it is, whatever needs to use that macAddress variables needs
to wait for it to come back. There's really no way around that. So rather
than trying to get the macAddress value available *outside* of your
callback there, whatever needs to use the macAddress would need to use it
inside of that callback (as you observed in your "In the function" console
statement).
Another option would be to use a promise. Are you familiar with those?
You'd end up with something like:
macAddressPromise = new Promise(function (resolve, reject) {
macGetting.getMac(function (err, macAddress) {
if (err) return reject(err)
return resolve(macAddress)
})
})
macAddressPromise is now a variable that you can pass around to wherever.
To then actually get the address itself instead of a Promise for the
address, you'd just call:
macAddressPromise.then(function (macAddress) {
// inside of here you have access to the actual address
})
Am I explaining this well?
On Wednesday, May 3, 2017 at 3:55:59 PM UTC-6, Peter B. wrote:
>
> Hi everyone,
>
> I need your help. I'm working on a project and I have a problem with the
> fact that nodejs is asynchronous.
> I want to export my variable called 'macAd'. But even with a global
> variable, I can't do it.
>
> Can you help me please ?
>
> Here is my code (I use ExpressJS Framework) :
>
>
> <https://lh3.googleusercontent.com/-BXG82Pb7DvE/WQncPQyRVpI/AAAAAAAAGL4/J4pWSrZiCAcjtKlVFSF1zbo1XsPqlNjPgCLcB/s1600/pic1.png>
>
>
>
>
>
>
>
>
>
>
>
>
> I have this output :
>
>
> <https://lh3.googleusercontent.com/-avHJAdkS-tc/WQncURfO0pI/AAAAAAAAGL8/RbV7NFxQDkI22aD0XKOJWpk_wb6EWRhBACLcB/s1600/pic2.png>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Thank you.
>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/14f5a1e6-d477-4345-b9d3-4051c75daa27%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.