0

What's the difference between chained .then and next function call, for example:

funcThatReturnsPromise().then((a) => {

}).then(doSomething)

With

var b;
funcThatReturnsPromise().then((a) => {
   b = a;
});
doSomething(b);

EDIT I've tried it, it does the same:

var p2; var p1 = new Promise(function(resolve, reject) {
  resolve(p2 = 'test1');
});
p1.catch(function(e){console.log('b',e)});
console.log('c',p2);
// result: c test1

vs

var p1 = new Promise(function(resolve, reject) {
  resolve('test2');
});
p1.then(console.log).catch(function(e){console.log('b',e)});
// result: test2
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • 3
    it's the difference between knowing how asynchronous code works and not - i.e, b is undefined in the second code block – Jaromanda X Apr 25 '17 at 07:38
  • It's a matter of execution order. In your first example, the `then`s will be executed only when the previous promise is resolved, whenever this promise in sync or async. In the second example, funcThatReturnsPromise will be executed almost at the same time than doSomething(b); and the `then` will *probably* be executed later. So, if doSomething is dependant of the result of funcThatReturnsPromise, you'll have to chain it. If it's totally independant, then you can execute it *at the same time*. – Booster2ooo Apr 25 '17 at 07:48

0 Answers0