guest
guest

Reputation: 2224

Asynchronous operations and promises - what is the difference between these ways of writing code

Saw this as an interview question. What are special consequences of writing promises these ways? Some of these will do the same thing, but what is the special conditions I need to pay attention to?

  1. If successful, do doSomethingElse(); since the inner function is done with currying, you can add special custom parameters.

    doSomething().then(function () {
      return doSomethingElse(a);
    });
    
  2. If successful:

    doSomething().then(function () {
      doSomethingElse(a);
    });
    
  3. doSomethingElse() is a call back function, and this is same as 4(???)

    doSomething().then(doSomethingElse());
    
  4. callback function doSomethingElse(), same as 3 (?)

    doSomething().then(doSomethingElse);
    

Upvotes: 0

Views: 95

Answers (2)

Jeff Bowman
Jeff Bowman

Reputation: 95644

Pay attention to when doSomethingElse is called, what its parameter is, and whether that gets passed along to additional chained promises.

1

doSomething().then(function () {
  return doSomethingElse(a);
});

This will:

  1. Call doSomething() immediately.
  2. Wait for the Promise returned by doSomething() to resolve.
  3. Call doSomethingElse() with the custom argument a, presumably from an outer scope. Anything chained after that will receive the return value of doSomethingElse(a).

2

doSomething().then(function () {
  doSomethingElse(a);
});

This will:

  1. Call doSomething() immediately.
  2. Wait for the Promise returned by doSomething() to resolve.
  3. Call doSomethingElse() with the custom argument a, presumably from an outer scope. Anything chained after that will receive undefined, not the return value of doSomethingElse.

3

doSomething().then(doSomethingElse());
  1. Call doSomething() immediately.
  2. Call doSomethingElse() immediately.
  3. Wait for the Promise returned by doSomething() to resolve.
  4. Call whichever function doSomethingElse returned in step 2 with one parameter, the value returned by doSomething in step 3. The return value of this function will be passed along to anything chained.

4

doSomething().then(doSomethingElse);
  1. Call doSomething() immediately.
  2. Wait for the Promise returned by doSomething() to resolve.
  3. Call the function doSomethingElse, very much like your example #1, except that the function is called with doSomething()'s value rather than your own a value. The return value of doSomethingElse is made available for chaining.

Upvotes: 1

Bsalex
Bsalex

Reputation: 2970

1 vs 2

In case 1 - you can get the result of doSomethingElse(a) in the next then in you promise chain.
Like this:

doSomething().then(function () {
  return doSomethingElse(a);
}).then(function (doSomethingElseWithAResult /* <-- here */) {
  // Some code
});

In case 2 - you always get undefined in the next then in you promise chain:

doSomething().then(function () {
  return doSomethingElse(a);
}).then(function (alwaysUndefined /* <-- here */) {
  // Some code
});

1 vs 3

You could rewrite 3 like this:

doSomething().then(function (doSomethingResult) {
  return doSomethingElse(doSomethingResult);
});

So 3 is close to 1, but in case 3 we do not provide special custom parameters to doSomethingElse and we pass the result of doSomething to doSomethingElse instead of a.

1 vs 4

The same as 1 vs 3, assuming that doSomethingElse is a curried function at least 2 arguments.

2 vs 3

Remembering that we could rewrite 3 like this:

doSomething().then(function (doSomethingResult) {
   return doSomethingElse(doSomethingResult);
});

The difference is that in case 2 we are not providing special custom parameters to doSomethingElse (see 1 vs 3) and we don't pass the result of doSomethingElse to the next then in our promise chain (see 1 vs 2).

2 vs 4

The same as 2 vs 3, assuming that doSomethingElse is a curried function at least 2 arguments.

3 vs 4

They are the same, assuming that doSomethingElse is a curried function of at least 1 argument. They are not the same if doSomethingElse is not a curried function.

Upvotes: 2

Related Questions