Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS getting Promise result from Console

If I want to get the result of a Promise from my node-testing console, how would I do that?

eg.

let promise = new Promise(() => {
  console.log('my promise');
}
promise.then(() => { console.log('resolved') })
// returns a Promise {<pending>}

await promise.then(() => { console.log('resolved') })
// still returns just a Promise

(async () => {
    await promise
})()
// still returns ... just a Promise

Ultimately I'm trying to test promise results (database queries) from my node console at a breakpoint, and I just keep getting Promises returned.


UPDATE - I guess this is more complicated than I thought. Because know one has been able to answer.

I understand how to get the results of a promise in a regular environment. I'm talking about getting results at a breakpoint while debugging a Node application. To connect to the console I'm referring to please follow these directions: https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27

From the console in DevTools, promises keep returning Promise {}. I do not know if it is possible to get the value of the promise, but if anyone knows either way please let me know.

like image 378
webbyweb Avatar asked Oct 20 '25 03:10

webbyweb


1 Answers

when you create a promise object you should use this syntax

var promiseObj = new Promise(executor);

the executor is a function with this signature

function(resolutionFunc, rejectionFunc){
    // typically, some asynchronous operation.
} 

When we go back to your specific example, you can define it as

let promise = new Promise( resolve => {
    resolve("my promise")
})

Note I havent added the reject function

then you can do

promise.then(value => console.log(value))

you can find a detailed description here

like image 177
Henok Teklu Avatar answered Oct 21 '25 17:10

Henok Teklu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!