1

My question is a bit regards concept.

A lot of times there is this such situation:

if(something){
    someAsyncAction();
}else{
    someSyncAction();
}

// Continue with the rest of code..
var a = 5;

The problem with this such case is clear, i don't want the var a = 5 to be call unless someAsyncAction() or someSyncAction() will done, now, cause soAsyncAction() is asynchronous the only way (i can think of) to solve this situation is something like that:

var after = function(){
    // Continue with the rest of code..
    var a = 5;
}

if(something){
    someAsyncAction(after);
}else{
    someSyncAction();
    after ();
}

BUT, this code is ugly, hard to read and looks like anti-pattern and problematic.

I trying to think maybe i can find some solution for that with Promises (using Bluebird at the backend) but can't find something.

Is anyone faced this before and can help me figure it out?

Thanks!

Shlomi
  • 3,622
  • 5
  • 23
  • 34
  • 1
    Mess with AsyncAction and SyncAction looks like anti-pattern to me too. – xdazz Apr 22 '15 at 15:36
  • Can be ugly if you want but is the way javascript callbacks are defined and used. I don't know any other way to work asyncronously. Oh and it is not anti-pattern at all, this IS the pattern. – Bolza Apr 22 '15 at 15:36
  • @xdazz. Async and Sync is very popular, for example: `if(!modelAlreadyExist) loadItAsyncFromDb() else useTheCurrentOneSync()` – Shlomi Apr 22 '15 at 18:51
  • @bolza, the fact that you can do something in JavaScript is not mean it's ok, like every other language, the problem with JavaScript (and other async languages) that lot of people write it very ugly without chance to debug it later.. this is why the patterns are so important here.. See below Bergi's answer, this is it! – Shlomi Apr 22 '15 at 18:56
  • Here's a very related question/answer and possible duplicate: [Return value from asynchronous OR synchronous JavaScript request](http://stackoverflow.com/questions/29779492/return-value-from-asynchronous-or-synchronous-javascript-request/29779522#29779522). – jfriend00 Apr 22 '15 at 21:00
  • possible duplicate of [if-else flow in promise (bluebird)](http://stackoverflow.com/q/26599798/1048572) – Bergi Jun 16 '15 at 21:54

1 Answers1

4

With promises, you would have a similar pattern as with the callback, only you would store the result first and not have to call/pass the callback twice:

function after(result) {
    // Continue with the rest of code..
    var a = 5;
}
var promise;
if (something){
    promise = someAsyncAction();
} else {
    promise = Promise.resolve(someSyncAction());
}
promise.then(after);

Or in short, you'd use the conditional operator and structure it much more straightforward:

(something
  ? someAsyncAction()
  : Promise.resolve(someSyncAction())
).then(function(result) {
    // Continue with the rest of code..
    var a = 5;
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I just learned that the ternary operator is also called the conditional operator. On another note, once the `yield` operator will be widely supported, `result = something? yield someAsyncAction() : someSyncAction()` will be possible as long as invoked in a managed generator context. – plalx Apr 23 '15 at 02:44
  • @plalx: will the `yeld` keyword have the same meaning as in python, where is used to create generators? – Cristik Apr 24 '15 at 19:07
  • @Cristik It will be used to create generators, yes. – plalx Apr 24 '15 at 20:10
  • @Cristik: Yes, exactly. Generators can be (ab)used to create "coroutines" that are run asynchronously. See [here](http://stackoverflow.com/q/23551418/1048572) for an explanation – Bergi Apr 24 '15 at 21:40