How can I access a output outside a promise `.then` method?

I am trying to return the output inside then and call it outside the promise function.

const fpPromise = import('https://openfpcdn.io/fingerprintjs/v3')
.then(FingerprintJS => FingerprintJS.load())
 fpPromise.then(fp => fp.get()).then(result => {
  console.log(result.visitorId)
  return result.visitorId;
 })


I tried to do [CASE 1] but not working

let visitorId; // Declare a variable in a broader scope

const fpPromise = import('https://openfpcdn.io/fingerprintjs/v3')
  .then(FingerprintJS => FingerprintJS.load());

fpPromise.then(fp => fp.get())
  .then(result => {
    visitorId = result.visitorId; // Assign the value to the variable
  })
  .catch(error => {
    console.error(error);
  });

// You can use `visitorId` anywhere outside the Promise chain
// However, note that it will be undefined until the Promise resolves
console.log(visitorId);

[CASE 2] but it also not worked

async function getVisitorId() {
  const fpPromise = import('https://openfpcdn.io/fingerprintjs/v3')
  .then(FingerprintJS => FingerprintJS.load())
  const fp = await fpPromise;
  const result = await fp.get();
  const visitorId = result.visitorId;
  // do something with the visitorId
  return visitorId;
}

const visitorId = await getVisitorId();

Please help how I achieve the goal

There is no where to return it to. Your global execution context, your main code is done/finished before your promises are resolved.

console.log('Global execution context started')

const myPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve("Hello there!");
  }, 0); // delay of 0ms
});

// setup a callback for when the promise is fullfilled using .then
myPromise.then((msg) => console.log(msg))

console.log('Global execution context finished now')

Output:

Global execution context started
Global execution context finished now
Hello there! // some time later

I am trying to find some useful tutorials. This might be a good start.

1 Like

so it means no chance to get the output outside in anyway

Unless I am missing something, no. You essentially rely on callback functions.

I edited my above post with a link. It might be worth having a read through it. It goes on in the next page to talk about the call stack and in the next page the event loop. This should hopefully give you an understanding of how things work.

On the right there is a menu with a section on promises, but I would start with those pages first.

If I find another good link I will post it.

This is kind of how I picture your last attempt

async function getFingerPrint() {
  const fpImport = await import('https://openfpcdn.io/fingerprintjs/v3'); // import?
  const fp = await fpImport.load();
  
  return await fp.get();
}

async function getVisitorId() {
  const fingerPrint = await getFingerPrint();
  
  return fingerPrint.visitorId;
}

async function mainFunc() {
  // can do this from inside of an async function
  const visitorId = await getVisitorId();
  // do stuff here
}

mainFunc(); // get things started

Not sure if this code works as you use fpPromise outside of the import.then but if it works you could do

getVisitorId()
{
    return new Promise(resolve, reject =>
    {
        const fpPromise = import('https://openfpcdn.io/fingerprintjs/v3')
                    .then(FingerprintJS => FingerprintJS.load());

        fpPromise.then(fp => fp.get())
              .then(result => {
                       resolve(result.visitorId); // Assign the value to the variable
               })
               .catch(error => {
                   reject(error);
              });
     });
}

try
{
    visitorId = await getVisitorId();
}
catch(error)
{
    console.log(error);
}

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