这是我的模块,其中x
返回为undefined
。
const si = require('systeminformation');
async function systemData() {
try {
let data = await si.system()
return {
manufacturer: data.manufacturer,
model: data.model
}
} catch (err) {
console.log(`${err}`)
}
}
function checkSystemData() {
(async () => {
let sd = await systemData()
if (sd.manufacturer === 'X') { // Many more such checks
} else {
return { check: false, manufacturer: sd.manufacturer }
}
})()
}
let x = checkSystemData()
console.log(x)
checkSystemData()
不能由其作者制作,async
是更大模块的一部分。我如何重构它以将x
接收为{ check: false, manufacturer: sd.manufacturer }
?
答案 0 :(得分:0)
您需要从checkSystemData()返回一些内容
return (async () => { // add return
let sd = await systemData()
if (sd.manufacturer === 'X') { // Many more such checks
} else {
return { check: false, manufacturer: sd.manufacturer }
}
})()
然后您就可以使用
checkSystemData().then(response => {
console.log(response)
})
或
let x = await checkSystemData()