webbyweb
webbyweb

Reputation: 435

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.

Upvotes: 4

Views: 4186

Answers (3)

webbyweb
webbyweb

Reputation: 435

From the comments I guess it is impossible to get asynchronous call results while at a breakpoint in JavaScript.

If anyone comes here and wants a way to be able to make DB queries from the Node console (REPL) like I was looking for, I'd recommend going here:

https://medium.com/@vemarav/build-rails-like-console-in-nodejs-repl-2459fb5d387b

Upvotes: 1

Ben Aston
Ben Aston

Reputation: 55729

The position of your breakpoints matters, and the behavior will be the same in the Node.js debugger and the browser debugger.

In the following I use labels like <b1> to identify breakpoint positions.

1.  const p1 = new Promise((resolve) => <b1> resolve('result 1'))
2.  <b2>
3.  const p2 = p1.then(() => <b3> 'result 2')
4.  <b4>

At <b1>, p1 will be undeclared because the executor function runs synchronously, and the variable declaration process has not yet completed.

At <b2>, p1 will be a fulfilled promise (Promise {<fulfilled>: "result 1"}), resolved with the value 'result 1' because resolve was called in the executor function.

The next breakpoint to be hit will be <b4>. Note: not <b3>.

At <b4>, p2 will be a pending promise (Promise {<pending>}) because the promise has been configured with a .then that has not yet had the opportunity to run. .then callbacks are run on asynchronous microtasks to give the programmer the opportunity to configure a promise chain ahead of its execution. After all, promises are designed to be used with asynchronous behavior.

On the next available microtask, <b3> will be hit as the .then callback is run. At <b3> the values of p1 and p2 remain unchanged. p1 was fulfilled earlier and will not change. The state of p2 is unchanged because the .then it was configured with has not yet completed running.

In order to observe p2 in its fulfilled state, you need to add a breakpoint to an extension of the promise chain.

let p1 = new Promise((resolve) => resolve('result 1'))
const p2 = p1.then(() => 'result 2')
p2.then(() => {
  // here `p2` === `Promise {<resolved>: "result 2"}`
  console.log(p2)
})

Upvotes: 0

Henok Teklu
Henok Teklu

Reputation: 538

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

Upvotes: 1

Related Questions