简体   繁体   中英

How do I get the result from this async function?

Problem

I'm trying to get the diskName value back from this.getDiskName('C:') and assign it to element['name'] :

getDisksInfo () { 
  ...
  element['name'] = this.getDiskName('C:')
  ...
},

getDiskName (diskLetter) {
  if (process.platform == 'win32') {
    var exec = require('child_process').exec
    var cmd = `wmic logicaldisk where caption="${diskLetter}" get VolumeName`

    exec(cmd, (err, stdout, stderr) => {
      if (err) {
        console.log(err)
      }
      let diskName = stdout.split('\n')[1]
      return diskName
    })
  }
}

I tried doing this, but I keep getting different errors:

getDiskName (diskLetter, callback) {
    ...
    exec(cmd, (err, stdout, stderr) => {
      if callback(null, () => {
        let diskName = stdout.split('\n')[1]
        return diskName
      })
    ...
}

Question

Could someone please explain how to return the value properly?

Your problem is that you are missing either a callback coming into getDiskName() or a Promise() coming out.

Since the Promise approach seems to be more popular nowadays, I'll go with that for this answer.

With a Promise approach, you need the function to return a Promise . In most cases, you just wrap all the code up in a Promise and return that:

getDiskName(diskLetter) {
    return new Promise((resolve, reject) => {
      // rest of your code in the function
    });
}

Then, instead of your return , you'll call resolve() :

let diskName = stdout.split('\n')[1];
resolve(diskName)

And for your error, you'll call reject:

if (err) {
  reject(err);
}

Then, in the function that uses it, you'll have to wait for the then() in your function:

this.getDiskName('C:').then(diskName => console.log(diskName))

The callback method is similar, you just pass in the callback into getDiskName and call it when you're ready.

This is a more idiomatic method to handle a case like this. We'll pass a function in to getDiskName which takes the disk name (which is the return value) as a parameter.

getDisksInfo () { 
  ...
  this.getDiskName('C:', function(diskName) {
    element['name'] = diskName;
  });
  // Note that code from here to the end doesn't have access
  // to element['name']
  ...
},

getDiskName (diskLetter, func) {
  if (process.platform == 'win32') {
    var exec = require('child_process').exec
    var cmd = `wmic logicaldisk where caption="${diskLetter}" get VolumeName`

    exec(cmd, (err, stdout, stderr) => {
      if (err) {
        console.log(err)
      }
      let diskName = stdout.split('\n')[1]
      func(diskName);
    })
  }
}

Now, this still might not work for you since perhaps you have code after the call which relies on knowing the diskName. In that case, you would probably roll that code into your anonymous function. Perhaps getDisksInfo takes a function as a parameter instead.

This is the general pattern, you have to determine how it best fits in your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM