====== Javascript Promises ====== Very details article about promises: https://web.dev/promises/ ===== Fact ===== - Support by Chrome 32, Opera 19, Firefox 29, Safari 8 & Microsoft Edge, and up - A Promise can be chained with ''.then(CALL_BACK_FUNCTION(VARIABLE){...})'' and ,''catch(CALL_BACK_FUNCTION(VARIABLE){...})'' - .then(...), and .catch(...) return type of Promise - A Promise can either be ''fulfilled or rejected'', so the ''.then(...)'' can actually take up to 2 call-back functions, the first one for fulfill, and the second one for rejection call-back. Like: fetch("https://api.example.com/items") .then(res => res.json()) .then( (result) => { this.setState({ isLoaded: true, items: result.items }); }, (error) => { this.setState({ isLoaded: true, error: error }); } ) - .catch(...) is the syntactic sugar for ''then(undefined, CALL_BACK_FUNCTION(VARIABLE){...})'' - The rejection call-back (second functional parameter of a Promise), and the .catch(...) are **different**! If we do .then(function(result){...}, function(error){...}), either one of them will be called. For .catch(...) if anything in the chain before it it rejected without a rejection call-back, any .then(...) in between will be skip, and the .catch(...) will be called.