1

Noob question. I clearly don't understand promises :-(. I have read extensively but I just cannot seem to get the 789 I originally set. So I have a basic piece of JavaScript that looks as follows:

<script type="text/javascript">

    async function WhatIsBrand() {
        let crGbApp = new OzCruisingPresentationConsumer.JavaScript.CrGbApp();
        crGbApp.SearchParameterDataManager.Data.SetBrand(789);
        let brand = await crGbApp.SearchParameterDataManager.Data.GetBrand();
        //console.log(brand.result);
        console.log('-- brand --');
        console.log(brand);
        console.log('-- /brand --');
        console.log('-- brand.result --');
        console.log(brand.result);
        console.log('/brand.result');
        return brand;
    }

    let brand2 = WhatIsBrand();
    console.log('******');
    console.log(brand2);
    console.log('******');
    console.log('######');
    console.log(brand2.result);
    console.log('######');

    console.log('@@@@@@@@@@@@');
    brand2.then(
        bogus => {
            console.log('2222---@@@@@@@@@@@@');
            console.log(bogus);
            console.log(bogus.result);
            console.log('/2222---@@@@@@@@@@@@');
        })
        .catch(err => console.error(err));
    console.log('@@@@@@@@@@@@');
</script>

Now in my web console I see as per the attached image:

enter image description here

EDIT1: @Phil, @CertainPerformance - Debugger will tell you the same thing:

enter image description here

and the promise is still pending and I get nothing back:

enter image description here

/EDIT1: @Phil

EDIT2: @CertainPerformance - Added 2222---@@@@@@@@@@@@ inside the then() - I am still back to bogus.result being null even though bogus contains the value I want for result which is 789:

enter image description here

/EDIT2: @CertainPerformance

So the value I want is there as somehow the console is completing the promise, I just cannot seem to do it in my own code.

So brand2 has a pending promise (yellow) which I need to get to completed. However I cannot seem to resolve the promise with either brand2.then() or Promise.resolve(brand2) as they both return errors.

So what do I "need to do" to brand2 so that I can get the value of result which is 789 instead if undefined (green)? Happy if that also means a simplification of WhatIsBrand().

TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • 2
    *as they both return errors.* What's the problem with `brand2.then`? What exactly is the error? That *is* the right way to do it - if the Promise is rejecting, then you need to handle the rejection, but it doesn't look like that's happening. `brand2.then((brand) => { console.log(brand); }).catch(...` – CertainPerformance Nov 18 '19 at 01:17
  • The console is a terrible debugging tool as it does not show a snapshot of data at any particular time. Use your browser's debugger to debug your code – Phil Nov 18 '19 at 01:21
  • 1
    I'm happy to do what I can to help you achieve what you want here, but the question *really is* a duplicate, this sort of thing gets asked multiple times every day - don't take it personally. Indicating that a question has been asked before does not imply anything about whether the question was asked in good faith or was researched, I'm sure it was. – CertainPerformance Nov 18 '19 at 01:49
  • 1
    The `.then` result isn't display in the middle of the `@@@`s because `.then` runs *asynchronously* (like after a `Promise.resolve`). Put the second `@@@` (or both of them) inside the `.then`. – CertainPerformance Nov 18 '19 at 01:49
  • @CertainPerformance Added 2222---@@@@@@@@@@@@ inside the then() - I am still back to bogus.result being null even though bogus contains the value I want for result which is 789. – TheEdge Nov 18 '19 at 02:13
  • 2
    For some reason, the object being returned to you from `GetBrand` is populated asynchronously. It's not *just* that you need to call `.then` on (or `await`) the Promise, but also that the `GetBrand` method appears to be buggy, because the object is not populated with the real `result` property when the Promise resolves. You can see here https://i.stack.imgur.com/USQn0.png that `null` is being logged *even inside your* `whatIsBrand`. For this to work, you should fix the `GetBrand` method so that it resolves only when the asynchronous action is fully complete. – CertainPerformance Nov 18 '19 at 02:19
  • Thanks @CertainPerformance I will check the GetBrand() – TheEdge Nov 18 '19 at 03:48

0 Answers0