2

I have the following node.js function call and function

var returned = checkCurrentProcesses()
    returned.then(() => {
        console.log(returned)

    })

function checkCurrentProcesses() {
    return new Promise(function(resolve, reject){
        exec('pgrep -u user123 -x node  -a', (err, cmdOutput, stderr) => {
            if (err) {
                reject({error:err})
            }
            else{
                resolve({output:cmdOutput})
            }
        });
    })

}

This successfully reruns the data I need, however it returns it in a way that I'm not understanding. I'm expecting to get a return of the resolved condition which is an object. But when I console.log(returned) the output looks as follows, and the data is an object in an object, and I'm unable to access it.

Promise { { output: '26278 node nodeMasterServer.js list\n' } }

Even if I simplify the code, the return is the same format.

function checkCurrentProcesses() {
    return new Promise(resolve =>{
        resolve({thisIs:"aTest"})
    })
}

Promise { { thisIs: 'aTest' } }

What is causing this behavior, and how can I fix it?

1 Answer 1

3

You're almost there, you need to pass the data to the then callback as so:

var returned = checkCurrentProcesses()
    returned.then((data) => { // can be any name you want
        console.log(data) // the object you are expecting

    })

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! output now shows correctly as { thisIs: 'aTest' }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.