1

I call a function,

getResponse(Math.random());
doSomethingWith(someVar);


getResponse(num) {
  if(num > 0.8) someVar = "whee";
  if(num > 0.5) someVar = "whoo";
  if(num > 0) callSomeOtherFunctionWhichDoesABunchOfDatabaseQueriesThatUsePromisesAndEventuallySetssomeVar();
}

Is there a way that I can wait for callSomeOtherFunctionWhichDoesABunchOfDatabaseQueriesThatUsePromisesAndEventuallySetssomeVar() to finish and set someVar ? I was thinking I could also wrap the assignments of someVar in a promise, to then make getResponse then()able, but that seems a bit excessive since I only have one case that I have a bunch of asynchronous work being done to determine someVar.

Austin Gayler
  • 4,038
  • 8
  • 37
  • 60
  • 5
    Promises are viral - once you started using it somewhere you cannot escape (and must use it everywhere). – zerkms Dec 14 '16 at 23:18
  • @zerkms terrifying. – Austin Gayler Dec 14 '16 at 23:22
  • 4
    Blaming that on Promises seems a bit harsh since the same would be true if it were callback-based. If you care about waiting for an async thing to finish, the function calling it is also going to need to be async. – loganfsmyth Dec 14 '16 at 23:26
  • 1
    http://stackoverflow.com/questions/28921127/how-to-wait-for-a-javascript-promise-to-resolve-before-resuming-function – manonthemat Dec 14 '16 at 23:49
  • 3
    It would be worse if your API returns a promises in some cases and another value in other cases. Promises provide a nice contract between APIs and allows implementations to change without impacting consumers. – Felix Kling Dec 14 '16 at 23:54
  • @zerkms Promises are a just a symptom of the async virus. – zero298 Dec 15 '16 at 00:14
  • @zero298 - javascript has had it's async methods since Brendan was a boy :p – Jaromanda X Dec 15 '16 at 01:58

1 Answers1

4

Note, replaced callSomeOtherFunctionWhichDoesABunchOfDatabaseQueriesThatUsePromisesAndEventuallySetssomeVar with someFunc in code below :p

getResponse needs to return a promise, always

so

function getResponse(num) {
    return Promise.resolve()
    .then(() => {
        if (num > 0.8) {
            someVar = "whee";
            return;
        }
        if (num > 0.5) {
            someVar = "whoo";
            return;
        }
        if (num > 0) {
            return someFunc();
        }
    });
}

Please note - the logic in your original code would (almost) always run that long winded function, because Math.random() is almost always > 0

the rest of the code would simply be

getResponse(Math.random())
.then(() => doSomethingWith(someVar));

if someFunc resolves to the value it sets someVar to, you could make the code a little nicer

var getResponse = num => Promise.resolve()
.then(() => {
    if (num > 0.8) {
        return "whee";
    }
    if (num > 0.5) {
        return "whoo";
    }
    return someFunc();
});

getResponse(Math.random())
.then(someValue => doSomethingWith(someValue));

but that may not be useful if someVar is used in other ways

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87