Using then () in a promise

I use the promise in the code block below, 2 first then () I do not return any value, so I'm wondering if this is a function in the first, and then () called before the function in the next then (). Im using typescript in angular 2. Thank you: D

Promise.resolve() .then(()=>{ this.getListStatus(); }) .then(()=>{ return this._laundryServiceOrderService.findAll(true, offset, this.itemsPerPage, filterQuery) }) .then((response) => { this.orders = response.data; this.totalItems = response.totalItems; }) .catch(error => { FlashMessage.setError(error.message); }); 
+5
source share
1 answer

first then () I do not return any value, so I am surprised that this is a function in the first, and then () is called before the function in the next then ().

Yes it is. (This can be trivially verified with a debugger.)

It does not matter that it does not return a value; which is actually the same as doing return undefined . Thus, the next callback in the chain will see undefined as the permission value, but since this next callback does not care about the permission value, this is fine.

Here is a simple example demonstrating this:

 Promise.resolve("a") .then(result => { console.log("First callback got: " + result); // Gets "a" }) .then(result => { console.log("Second callback got: " + result); // Gets undefined return "b"; }) .then(result => { console.log("Third callback got: " + result); // Gets "b" }) .catch(error => { console.log("There no error above, this won't get triggered."); }); 
+6
source

Source: https://habr.com/ru/post/1261372/


All Articles