Factory async function with ability of cancelling
#js #promise #AbortController
#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
โ Article link
#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
๐1
#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
#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
#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
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
โค2๐1