Posts

Showing posts from 2022

Image Cloning problem Hackerrank

 Image Cloning  problem Hackerrank  'use strict' ; const  fs = require( 'fs' ); process.stdin.resume(); process.stdin.setEncoding( "ascii" ); let  inputString =  "" ; let  currentLine =  0 ; process.stdin.on( "data" ,  function  (chunk) {     inputString += chunk; }); process.stdin.on( "end" ,  function  () {     inputString = inputString.split( '\n' );     main(); }); function  readLine() {    return  inputString[currentLine++]; } class   Size  {      constructor (width, height) {          this .width = width;          this .height = height;     } } class   Image  {      // Add methods he...

Parking a lot problem solution Hackerrank

Javascript Parking a lot problem solution Hackerrank   'use strict' ; const  fs = require( 'fs' ); process.stdin.resume(); process.stdin.setEncoding( "ascii" ); let  inputString =  "" ; let  currentLine =  0 ; process.stdin.on( "data" ,  function  (chunk) {     inputString += chunk; }); process.stdin.on( "end" ,  function  () {     inputString = inputString.split( '\n' );     main(); }); function  readLine() {    return  inputString[currentLine++]; } class   ParkingLot  {   slots = [];    constructor (parkingSize) {      this .slots =  new   Array (parkingSize).fill( null );   }   park(carId) {     console.log( `Parking car:  ${carId} ` );      if ...

Async/await

  Async/await An async function is a function declared with the async keyword, and the await keyword is permitted within them. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains. "async and await make promises easier to write" async  makes a function return a Promise await  makes a function wait for a Promise  Async functions Let’s start with the async keyword. It can be placed before a function, like this: async function f() {   return 1; } The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically. For instance, this function returns a resolved promise with the result of 1; let’s test it: async function f() {   return 1; } f().then(alert); // 1 …We could explicitly return a promise, which would be the same: async function f() {   return Promis...

Promise.race() in javascript

Promise.race() The  Promise.race()  method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise. The Promise.race() method takes an iterable of promises as an input, and returns a single Promise that resolves into the first resolved or rejected promise. Example: const promise1 = () => new Promise((resolve,reject) => { setTimeout(() => { resolve(1) },100) }) const promise2 = () => new Promise((resolve,reject) => { resolve(2) }) const promise3 = () => new Promise((resolve,reject) => { setTimeout(() => { resolve(3) }) }) Promise.race([promise1(),promise2(),promise3()]) .then((val) => { console.log(val) // 2 }) .catch((err) => { console.log(err) })

Promise.allSettled()

  Promise.allSettled() The Promise.allSettled() method returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.  const promise1 = () => new Promise((resolve,reject) => { setTimeout(() => {     resolve(1) },100) }) const promise2 = () => new Promise((resolve,reject) => { reject(2) }) const promise3 = () => new Promise((resolve,reject) => { setTimeout(() => { resolve(3) }) }) Promise.allSettled([promise1(),promise2(),promise3()]) .then((val) => { console.log(val) // 3 }) .catch((err) => { console.log(err) }) // Output: // [ // { status: 'fulfilled', value: 1 }, // { status: 'rejected', reason: 2 },  // { status: 'fulfilled', value: 3 } // ]  const promise1 = Promise.resolve(3); const promise2 = new Promise((resolve, reject) => setTimeout(reject, 100, 'foo')); const promises = [promise1, promise2]; Promise.allS...

Promise.any()

Promise.any() takes an iterable of Promise objects. It returns a single promise that fulfills as soon  as any of the promises in the iterable fulfills, with the value of the fulfilled promise. If no  promises in the iterable fulfill (if all of the given promises are rejected), then the returned  promise is rejected with an AggregateError, a new subclass of Error that groups together  individual errors.   const promise1 = Promise.reject(0); const promise2 = new Promise((resolve) => setTimeout(resolve, 100, 'quick')); const promise3 = new Promise((resolve) => setTimeout(resolve, 500, 'slow')); const promises = [promise1, promise2, promise3]; Promise.any(promises).then((value) => console.log(value)); // expected output: "quick" const promise1 = () => new Promise((resolve,reject) => { setTimeout(() => { resolve(1) },100) }) const promise2 = () => new Promise((resolve,reject) => { reject(2) }) const promise3 = () => new Promise((resolv...

promise.all() method in javascript promise

  The  Promise.all()  method takes an iterable of promises as an input, and returns a single  Promise  that resolves to an array of the results of the input promises. This returned promise will fulfill when all of the input's promises have fulfilled, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error. const promise1 = () => new Promise((resolve,reject) => { setTimeout(() => { resolve(1) },100) }) const promise2 = () => new Promise((resolve,reject) => { resolve(2) }) const promise3 = () => new Promise((resolve,reject) => { setTimeout(() => { resolve(3) }) }) Promise.all([promise1(),promise2(),promise3()]) .then((val) => { console.log(val) // [1,2,3] }) .catch((err) => { console.log(err) })

Promise Methods

 There are some methods defined inside of the Promise API which helps us deal with a bulk of promises at once: Promise . race ( ) ● Promise . all ( ) ● Promise . any ( ) ● Promise . allSettled ( ) Promise . race ( ) : The Promise . race ( ) method takes an iterable of promises as an input , and returns a single Promise that resolves into the first resolved or rejected promise . Example : const promise1 = ( ) => new Promise ( ( resolve , reject ) => { setTimeout ( ( ) => { resolve ( 1 ) } , 100 ) } ) const promise2 = ( ) => new Promise ( ( resolve , reject ) => { resolve ( 2 ) } ) const promise3 = ( ) => new Promise ( ( resolve , reject ) => { setTimeout ( ( ) => { resolve ( 3 ) } ) } ) Promise . race ( [ promise1 ( ) , promise2 ( ) , promise3 ( ) ] ) . then ( ( val ) => { console . log ( val ) // 2 } ) . catch ( ( err ) => { console . log ( err ) } ) Promise . all ( ) : The Promise . all (...