1

From a tutorial book I the following code

  createShoppingList: (store, shoppinglist) => {
    return api.addNewShoppingList(shoppinglist).then(() => {
      store.dispatch('populateShoppingLists')
    }, () => {
      store.commit(types.ADD_SHOPPING_LIST, shoppinglist)
    })
  }

notice the comma after the .then() block

Is it equivalent to a chained .then() ?

  createShoppingList: (store, shoppinglist) => {
    return api.addNewShoppingList(shoppinglist)
    .then(() => {
      store.dispatch('populateShoppingLists')
    })
    .then(() => {
      store.commit(types.ADD_SHOPPING_LIST, shoppinglist)
    })
  }

or is it only a block inside the .then() ? like :

return api.addNewShoppingList(shoppinglist)
.then(
() => { store.dispatch('populateShoppingLists')}, 
() => { store.commit(types.ADD_SHOPPING_LIST, shoppinglist) }
)

thanks for feedback

  • 4
    No, the comma isn't AFTER .then() ... it's within it ... the second callback function to .then is what is called on rejection – Jaromanda X Sep 10 '17 at 10:27
  • ok thanks... so it seems there is a bug in thi code as BOTH store.commit and store.dispatch should be performed when the apI.addNewShoppingList is resolved ... in this oredr . commit then dispatch..) –  Sep 10 '17 at 10:38
  • perhaps, who knows, without full context – Jaromanda X Sep 10 '17 at 10:40
  • yes, but in the book context, qfter adding the shoppingList ( which save it to the server: ShoppingListsResource.save(data) ) then it must be committed into the local store then re-display the new list by executing the action 'populateShoppingLists' with a dispatch... –  Sep 10 '17 at 10:46
  • The third snippet is exactly the same as the first, just with a few different linebreaks. Yes, it's obviously different from the second one. – Bergi Sep 10 '17 at 12:01

2 Answers2

2

No

.then(resolved, rejected)

is not equal to

.then(resolve)
.then(rejected)// :/

its rather similar to:

.then(resolved)    
.catch(rejected)

(Theres still the difference that a rejection inside of then will be catched now, while the upper version is uncatched)

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • nope, it's more like `.catch(rejected).then(resolved)`, but not entirely. You'll notice the difference if your `resolved()` method throws another Error; `rejected` won't catch it here. – Thomas Sep 10 '17 at 10:45
  • @thomas no, that will always enter the then... :/ – Jonas Wilms Sep 10 '17 at 11:19
  • But if the `then()` throws an Error, the `catch()` will catch it, and you get a resolved Promise. While with `.then(a,b)` an Error in `a()` won't be catched and get a rejected Promise. IMO `.catch(b).then(a)` is closer to `.then(a,b)` but it still ain't the same thing, as here you will inject the value returned vby the `catch()` block into the `then()`. Ultimately `then(a,b)` is comparable to a condition, as to only one side will be executed, while either with `.catch(b).then(a)` or with `.then(a).catch(b)` both sides can be executed. Check out the snippet in my answer. – Thomas Sep 10 '17 at 11:59
  • @Jonasw [It's superficially "similar" at best](https://stackoverflow.com/q/24662289/1048572). – Bergi Sep 10 '17 at 12:02
  • @bergi i know. Thats why *Theres still the difference that a rejection inside of then will be catched now, while the upper version is uncatched* – Jonas Wilms Sep 10 '17 at 12:37
  • @thomas whats your point? I know that. And *IMO* is a good reason to add your own answer which you did, but i doesnt justify the *nope* – Jonas Wilms Sep 10 '17 at 12:39
0

No,

.then(resolved, rejected)

is more like

.catch(rejected)
.then(resolved)

but not entirely, as with .then(resolved, rejected) the result of this function is forwarded to the following Promise, and never to each other.

var resolved = value => `resolve(${value})`;
var rejected = err => `catched(${err})`;
var throwing = () => { throw "thrown Error" };

Promise.resolve(42)
  .then(resolved, rejected)
  .then(v => console.log("then(a,b) => ", v));
  
Promise.reject(42)
  .catch(rejected)
  .then(resolved)
  .then(v => console.log("catch(b).then(a) => ", v));
  
//and that's why .then(a,b) ain't like .then(a).catch(b)
Promise.resolve(42)
  .then(throwing, rejected)  
  .then(v => console.log("then(throw,b) => ", v))
  .catch(err => console.log("then(throw,b) => ", err));


Promise.resolve(42)
  .then(throwing)
  .catch(rejected)  
  .then(v => console.log("then(throw).catch(b) => ", v))
  .catch(err => console.log("then(throw).catch(b) => ", err));
.as-console-wrapper{top:0;max-height:100%!important}
Thomas
  • 11,958
  • 1
  • 14
  • 23