Angular ๐Ÿ‡บ๐Ÿ‡ฆ - practical notes
1.63K subscribers
1.6K photos
1 file
532 links
Angular - practical notes

This group is for posting practical notes for Angular developers. Mostly all posts are for quick implementation https://t.me/angular_practical_notes (Commenting on posts only in ENG and UA langs here). Welcome!
Download Telegram
Factory async function with ability of cancelling

#js #promise #AbortController

function asyncWithAbort(promiseFn, signal){
if (signal?.aborted){
return Promise.reject(new DOMException("Aborted", "AbortError"));
}
return new Promise((resolve, reject) => {
promiseFn().then(resovle).catch(reject);

signal?.addEventListener("abort", () => {
reject(new DOMException("Aborted", "AbortError"));
});
});
}

โœ… Link: https://javascript.plainenglish.io/best-practices-for-using-abortcontroller-87892b72d07e
๐Ÿ“„ Real Examples of Promise Patterns

#js #promise

โ€” Promise.all() is ideal when you have multiple asynchronous operations that are independent of each other and need to be performed concurrently.

โ€” Promise.race() Use Promise.race() when you need to respond to whichever promise resolves or rejects first. It's useful in scenarios like timeout implementations.

โ€” Promise.allSettled() is useful when you need to wait for all promises to settle, regardless of whether they resolve or reject.

โ€” Promise.any() returns the first promise that resolves and ignores rejections unless all promises reject.


โœ… Article link
โค1๐Ÿ‘1๐Ÿ”ฅ1
๐Ÿ“„ Async/Await Techniques

#js #promise #async #await

โœ… Article link
๐Ÿ‘2๐Ÿ‘Ž1
๐Ÿšณ Interview Questions: Promise object

#js #interview #promise

Problem: Implement a simple version of Promise:

The Promise object is an asynchronous programming solution for handling asynchronous events. Promise objects can represent the status of an asynchronous operation, including:

- pending
- fulfilled
- rejected

Analysis:

๐ŸŒถThe MyPromise class is a custom Promise class whose constructor accepts an executor function as a parameter.
๐ŸŒฝThe executor function in the constructor will be executed immediately and accepts two parameters, resolve and reject, which are used to modify the state of the Promise.
๐ŸžThe resolve method is used to modify the Promiseโ€™s status from โ€œpendingโ€ to โ€œfulfilledโ€ and pass the value to subsequent handlers.
๐ŸฅšThe reject method is used to modify the Promiseโ€™s status from โ€œpendingโ€ to โ€œrejectedโ€ and pass the reason to the subsequent handler.
๐ŸณThe then method is used to register a callback function to be executed when the Promise is completed or rejected. It accepts two parameters: onFulfilled and onRejected, which are called when the Promise is completed or rejected respectively.
๐ŸฅฉThe then method returns a new MyPromise instance to support chained calls. If onFulfilled or onRejected returns a value, it will be used as the resolved value for the next MyPromise instance.
๐Ÿ”The catch method is the shorthand form of then(null, onRejected).
๐Ÿ•The isFulfilled method is used to check whether the Promise is in the fulfilled state.
๐ŸŒฎThe isRejected method is used to check whether the Promise is in the rejected state.
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿ‘4
๐Ÿšณ Interview Questions: Implement promise.all method

#js #interview #promise

Analysis: Use the Promise.all principle to synchronize the Promise state through the counter and result array.
Please open Telegram to view this post
VIEW IN TELEGRAM
๐Ÿ“ต Create a Cancellable Promise

#js #promise

Create a cancellable promise P with a method cancel such that after we call the method P.cancel(), the promise cannot be resolved now. There should be a way to know that the promise was cancelled and not rejected.


โœ… Article link
Please open Telegram to view this post
VIEW IN TELEGRAM