Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lwc:programming:javascript [2022/11/15 08:35] – John Harrison | lwc:programming:javascript [2025/03/18 13:58] (current) – John Harrison | ||
---|---|---|---|
Line 52: | Line 52: | ||
* to see all the event messages flying around: '' | * to see all the event messages flying around: '' | ||
* then to unsee: '' | * then to unsee: '' | ||
+ | |||
+ | ==== Promises ==== | ||
+ | Promises are always async and often used to prevent UI blocking when waiting for something like an I/O interface. If a function has a '' | ||
+ | === Creating the promise (producing) === | ||
+ | < | ||
+ | myPromise = Promise(success, | ||
+ | // do something that takes some time here | ||
+ | if (succeeded) { | ||
+ | success() { // what to return on success i.e. " | ||
+ | else | ||
+ | failure() { // what to return on failure }; // this is not a function but only a return value | ||
+ | } | ||
+ | </ | ||
+ | === Calling or running the promise (consuming) === | ||
+ | == Using .then(onFulfilled, | ||
+ | //this is the less preferred method// | ||
+ | < | ||
+ | myPromise.then( | ||
+ | successFunction(value) { // do stuff here with value which was returned by ' | ||
+ | failFunction(value) { // do stuff here with value returned by ' | ||
+ | </ | ||
+ | == Using .catch == | ||
+ | // this is best practice in general // | ||
+ | < | ||
+ | myPromise.then( | ||
+ | successFunction(value) { // do stuff here with value returned by ' | ||
+ | .catch(failFunction(value) { // do stuff here with value returned by ' | ||
+ | </ | ||
+ | ==== Arrow Notation ==== | ||
+ | ^ Category ^ Old Skool ^ Arrow Function | ||
+ | | No Parameter | ||
+ | | One Parameter | ||
+ | | Two Parameters |