0

How to get the output as a (global) variable from Promises in JavaScript? I found many answers but none helped me in applying to my problem. Specifically, I want to get the data from the code below. It is the node-os-utils library.

 cpu.usage()
  .then(info => {
    console.log(info)
  })  

EDIT:

I tried to edit the code according to your recommendations, unfortunately I still couldn't find where I'm making a mistake.

I used the async function with await. Now I would like to return and display the value in systemdata, but the output shows Promise <pending>. I figured it's probably because the object is running in the stack before promise completes.

cpu.usage()
  .then(cpuPercentage => {
    return cpuPercentage +'%';
  });

const printCpuUsage = async () => {
    const a = await cpu.usage();
    return a;
  };
let systemdata = {
  cpuCount: cpuCount,
  cpuModel: cpuModel,
  cpuUsage: printCpuUsage(),
  // memoryUsage: ,
  // CPUtemp: ,
  // batteryCycle: ,
  // StorageSize: 
};

console.log(systemdata)

So I tried to put async directly into the object. With the assumption that this way the object property will have to wait for a promise.

const cpuusage = cpu.usage()
  .then(cpuPercentage => {
    return cpuPercentage +'%';
  });

let systemdata = {
  cpuCount: cpuCount,
  cpuModel: cpuModel,
  cpuUsage: async () => {
    const a = await cpuusage;
    return a;
  },
  // memoryUsage: ,
  // CPUtemp: ,
  // batteryCycle: ,
  // StorageSize: 
};

console.log(systemdata)

Unfortunately this code output: cpuUsage: [AsyncFunction: cpuUsage]

  • 2
    You're already showing _exactly_ how to get the value - access it in the `.then` callback. Your specific code is really irrelevant, because that's how you get the value from _all_ promises (either that, or `await` them). – jonrsharpe Apr 27 '22 at 20:27
  • 1
    You use the value you got INSIDE the `.then()` callback right where you `console.log(info)` is. Nothing to change here. Should work just like this. – jfriend00 Apr 27 '22 at 20:38

1 Answers1

-2

try this:


// global scope
let globalInfo;
cpu.usage().then(info => {
  globalInfo = info;
});

Alan H.
  • 16,219
  • 17
  • 80
  • 113
Kasi
  • 92
  • 11
  • 1
    Almost everyone attempting to use this answer is immediately going to run into this problem: https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – Quentin Apr 27 '22 at 20:37
  • This is the correct answer. Keep in mind that the value will not be available synchronously (immediately) in the global variable. Probably, you should do whatever you want to do after the promise resolves, instead of putting the answer into a global variable, but it’s hard to know without more context. – Alan H. Apr 27 '22 at 20:38
  • @AlanH. this is just an antipattern, not a *this is the correct answer*. But nice from you for providing more correct context. – Roko C. Buljan Apr 27 '22 at 20:40
  • This is rarely the right way to use asynchronous results because nobody outside of the `.then()` handler will have any idea when the value is actually correct and available. – jfriend00 Apr 27 '22 at 20:40
  • It’s the correct answer to the question as it was asked, but as the rest of my comment says, yeah, it’s an antipattern and probably the wrong answer to the actual problem – Alan H. Apr 27 '22 at 20:40