Skip to content

Instantly share code, notes, and snippets.

View mhm13dev's full-sized avatar
💥
Fullstack Web Developer

Mubashir Hassan mhm13dev

💥
Fullstack Web Developer
View GitHub Profile
@mhm13dev
mhm13dev / promises-vs-observables.md
Created November 17, 2023 08:20
Promises vs Observables in JavaScript

Promises and observables are both mechanisms used in JavaScript for handling asynchronous operations, but they have different characteristics and usage patterns.

Promises:

  • Promise is an object representing the eventual completion or failure of an asynchronous operation and its resulting value.
  • It has states: pending (initial state), fulfilled (operation completed successfully), or rejected (operation failed).
  • A Promise can be created using the new Promise() constructor, taking a function with resolve and reject parameters.
  • It provides a cleaner way to write asynchronous code compared to callbacks, allowing chaining of .then() and .catch() methods.
  • .then() is used to handle the resolved value, and .catch() is used to catch any errors.
  • Promises are not cancellable, and once settled (fulfilled or rejected), they remain in that state.
@mhm13dev
mhm13dev / axios.refresh_token.1.js
Last active August 20, 2023 10:21 — forked from Godofbrowser/axios.refresh_token.1.js
Axios interceptor for refresh token when you have multiple parallel requests. Demo implementation: https://github.com/Godofbrowser/axios-refresh-multiple-request
// for multiple requests
let isRefreshing = false;
let failedQueue = [];
const processQueue = (error, token = null) => {
failedQueue.forEach(prom => {
if (error) {
prom.reject(error);
} else {
prom.resolve(token);

Keybase proof

I hereby claim:

  • I am mhm13dev on github.
  • I am mhm13dev (https://keybase.io/mhm13dev) on keybase.
  • I have a public key ASBOojcYrzhO0akkcDGxpym4H5RY7GX8aL7J62Aqmczz6wo

To claim this, I am signing this object: