I have a function that contains a promise. The function do an HTTP request and I want to resolve it and return the response as a value in a variable.
function sendCommand(auth, playerId, command) {
return new Promise((resolve, reject) => {
request.post(`https://test.com/${playerId}/command`,{
headers: { Authorization: auth },
json: {
"arg": `${command}`
}
}, function status(err, response, body) {
if (response.statusCode === 200) {
resolve(body)
} else {
reject(err)
}
}
)
})
}
I run this function from a different file so I do the following:
module.exports = {
doSomething: (playerId, command) => {
loginHelper.getToken().then(token =>
sendCommand(token, playerId, command))
}
}
On the other file, I want to store the resolved response in a variable such as this:
const commandHelper = require(__helpers + 'command-helper')
let test = commandHelper.doSomething(playerId, 'command')
I expect the test variable to contain response data.