JS class and asynchronous
23 Aug (class)
- js only: vari = func --> vari() is the same func
- property and method (vari and func)
- Why do we need to use class? When we have to code json multiple times. However, using class, it became simple. Furthermore, you can assign values in a very simple way using constructor
- You can add methods as an object
- '#' -> vari, 'no #' -> getter setter
- getter and setter gets value as a para
- this. -> gets things from the class.
28 Aug (async)
- When you return Promise() from a function, you are returning an object that is an instance of the Promise class, not the class itself. There is no explicit name given to the object being returned inside the function itself before you assign it to a variable like promiseObject
- Promise() is another class you have to learn. For the parameter, a callback function. In the callback function, when succeed, resolve(). Only one para for resolve(). Should be a json if multiple data. The method then() is linked to resolve(). Gets the para from resolve().
- When you want to use a callback function, use ‘.then(the callback function)’. Another callback? Use .then() again at the end and so on.
- Promise is a class, so you need to create an object to use it. Then() is a method in Promise, rmbr? And you can just return an object without a name in a function, so you can use that function to create an object with a name later.
- Async returns Promise.resolve but also, await returns Promise.resolve. Async is for function, whereas await is for a variable. Await should be in async function.
- When to use ‘then’ than ‘await’ :useful when you need to chain multiple asynchronous operations without nesting them inside an async function.