2

Here is my function which returns a promise containing text value of a button

getToggleViewButtonText(){
        return this.toggleBasicOrAdvancedView.getText()
    }

Now, I wrote one more function which takes other functions as a parameter and resolves the promise and returns its value.

promiseResolve(func){
    return func.then(value=>{
         return value
    });

Problem is when I use this

promiseResolve(this.getToggleViewButtonText())

I get promise back instead of text value of button element.But, if I do console.log(value) in promiseResolve function. I can see the value is there. Can some help where I am going wrong here.

lfc1988
  • 67
  • 1
  • 8
  • *"I get promise back instead of text value of returned"* - why did you expect anything different? The whole point of the promise is that it's wrapping *asynchronous* behaviour, so you have to access the resolved value in the callback. – jonrsharpe Oct 12 '17 at 19:53
  • Why is use of `Promise` necessary? What does `this.toggleBasicOrAdvancedView.getText()` return ? – guest271314 Oct 12 '17 at 19:54
  • Possible duplicate of [return value after a promise](https://stackoverflow.com/questions/22951208/return-value-after-a-promise) – jonrsharpe Oct 12 '17 at 19:56
  • @jonrsharpe My question is if do console.log(value) I see the correct value getting printed which I think means promise got resolved at that point. But if I do return value and then use that outside like `promiseResolve(this.getToggleViewButtonText())`, I dont get value back. What is the reason? – lfc1988 Oct 12 '17 at 20:03
  • @guest271314 this return a promise which when resolved gives a text value of a button element. – lfc1988 Oct 12 '17 at 20:06
  • Why is `promiseResolve` function necessary? – guest271314 Oct 12 '17 at 20:08
  • @guest271314 I wanted to create a generic function which takes other function as a parameter and resolves the promise and returns its value. So that I don't have to resolve the promise every time I create a function. – lfc1988 Oct 12 '17 at 20:12
  • What is the issue with the code at Question? – guest271314 Oct 12 '17 at 20:13
  • In `promiseResolve(func)` if I use `console.log(value)`. I see the value getting printed but if I just do `return value`. It returns promise. I was thinking of using `promiseResolve(this.getToggleViewButtonText())` in an if statement directly to compare values but as its returning promise, I am not able to do that. – lfc1988 Oct 12 '17 at 20:17
  • I believe that [this answer](https://stackoverflow.com/a/22536838/1842492) explains promise pretty well... personally for me it is like Prigles commercial from the 90' "Once You Pop, You Can't Stop" so from `then` you won;t get anything other than yet another promise... you could try escape it in the syntax with `async-await` but it doesn't change a lot under the hood – gaa Oct 12 '17 at 20:20

2 Answers2

1

Promise.then() returns the promise itself. The point of the promise is that you don't know when it will resolve. So you can only get its results using async methods.

this.getToggleViewButtonText().then(value => {
    // Do something with the value here
});
Chris Phillips
  • 673
  • 5
  • 10
  • I don't want to resolve the promise here. And would like to resolve it in the second function by passing it as a parameter. – lfc1988 Oct 12 '17 at 20:08
  • You can't force it to resolve. The code that is calling `this.getToggleViewButtonText()` must be adapted to handle the async nature. – Chris Phillips Oct 12 '17 at 20:36
0

If the requirement is to get the value of a Promise within code at next line you can use async/await

function getToggleViewButtonText() {
  return Promise.resolve(1)
}


async function promiseResolve(func) {
  let value = await func;
  if (value > 1) {
    return value
  } else {
    return value + " is not greater than 1"
  }
}

promiseResolve(getToggleViewButtonText()).then(data => console.log(data))
guest271314
  • 1
  • 15
  • 104
  • 177
  • That has the same result, as `promiseResolve` is returning a promise – Chris Phillips Oct 12 '17 at 20:37
  • @ChrisPhillips Not sure what you mean? See clarification of inquiry by OP _"I was thinking of using `promiseResolve(this.getToggleViewButtonText())` in an if statement directly to compare values but as its returning promise"_ https://stackoverflow.com/questions/46717787/promise-value-not-getting-returned-but-if-i-do-console-log-it-prints-the-value/46718267?noredirect=1#comment80382671_46717787 – guest271314 Oct 12 '17 at 20:39
  • I believe that they want to have a function `promiseResolve` that isn't async and just returns the value. Your version of `promiseResolve` is still returning a promise, (the one create by async syntax) – Chris Phillips Oct 12 '17 at 21:29
  • @ChrisPhillips `let value = await func;` sets `value` to a value that is the result of the `Promise` – guest271314 Oct 12 '17 at 21:35
  • Yes that is true, but the last line in your example is still using a promise. So the outer code that calls `promiseResolve` still has to deal with a promise. Maybe if they showed us an example of how they wanted to use `promiseResolve` we could better form our replies – Chris Phillips Oct 12 '17 at 22:15
  • @ChrisPhillips Only if a value is expected to be returned from `promiseResolve()`, yes. Otherwise OP does not need to use `.then()` chained to `promiseResolve()` – guest271314 Oct 12 '17 at 22:22
  • This is how I am looking to use promiseResolve() to compare value in if statement `enableView(){ if(this.promiseResolve(this.getToggleViewButtonText())==="Show Advanced View"){ return this.toggleBasicOrAdvancedView.click(); } else { console.log('Already in Advanced View') } } ` – lfc1988 Oct 13 '17 at 03:21
  • You can use pattern at Answer to get the value of the `Promise` instead of `Promise` object itself – guest271314 Oct 13 '17 at 12:58
  • You would need to use an `async` function and `await` within the function to get the value of `this.resolvePromise()` to be able to pass the value to an `if` condition – guest271314 Oct 13 '17 at 13:20