0

On my journey of learning Promises in Javascript/Nodejs, I came a cross an issue while working on some code so I made a sample of my problem below. First, I have a calculation that takes a long time but at some point I need the answer before I can continue on with the rest of code. (On a side note, I am new to Javascript/Nodejs)

function add(x,y) {
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            console.log("1 + 1 =", x+y ); 
            resolve(x + y);
        }, 300);
    });
}

function calc(x,y)
{
    let answer;
    let promise = add(x,y);

    promise.then(function (value) {
        console.log("1 + 1 =", answer );
        answer = value;
    });

    return answer;
}

console.log('Calculation of 1 + 1 =', calc(1,1)); // I need an answer here. If I have to wait, so be it!
console.log('The correct answer is ',1+1);
console.log('The end.');

The Output
----------
Calculation of 1 + 1 = undefined
The correct answer is  2
The end.
1 + 1 = 2          
1 + 1 = undefined

PKonstant
  • 834
  • 2
  • 15
  • 32
  • 2
    Duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Tyler Roper Nov 06 '19 at 18:25
  • 1
    Promises are asynchronous. This means they run in the background. At some point in the future, when it's done, then it'll run the `then()` callback. By that point, your `calc()` function is long since finished. So, what's happening is that `calc()` is asking the promise to run and then it returns `answer` while the promise is in the background. – gen_Eric Nov 06 '19 at 18:25
  • 2
    Also change `setTimeout(function (x, y) {` to `setTimeout(function () {`. When `setTimeout` runs it does not pass any params to the function. So the `x` and `y` are being *replaced* with nothing. – gen_Eric Nov 06 '19 at 18:28
  • @Rocket, thanks for pointing that out. – PKonstant Nov 06 '19 at 18:30
  • @Rocket, I get what you are saying now so how can I have ```calc()``` wait for the promise to finish? – PKonstant Nov 06 '19 at 18:34
  • @Tyler, my question might be similar the one you referenced but I am still trying to figure out how to proceed with a calculation if I do not have all my values in yet. – PKonstant Nov 06 '19 at 18:43
  • 1
    @PKonstant add a promise inside the calc function. It seems like the calc function might be unnecessary. – Matt Kuhns Nov 06 '19 at 18:44
  • @Matt, do you mean wrap ```calc()``` in another promise? ```calc()``` already has a promise? – PKonstant Nov 06 '19 at 18:48
  • 1
    calc() is resolving the promise from add(x,y). You have a race condition in calc, it just returns immediately. – Matt Kuhns Nov 06 '19 at 18:49
  • 1
    function add(x,y) { return new Promise(function (resolve, reject) { setTimeout(function () { resolve(x + y); }, 300, x, y); }); } function calc(x,y) { return new Promise((resolve, reject) => { console.log(x,y); let answer; let promise = add(x,y); promise.then(function (value) { answer = value; resolve(answer); }); }) } calc(1, 1).then(result => { console.log('Calculation of 1 + 1 =', result); }) – Matt Kuhns Nov 06 '19 at 18:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201963/discussion-between-matt-kuhns-and-pkonstant). – Matt Kuhns Nov 06 '19 at 18:53

0 Answers0