Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

promise.all async await

async function fetchABC() {
  const [a, b, c] = await Promise.all([a(), b(), c()]);

} 
Comment

async await promise all javascript

let characterResponse = await fetch('http://swapi.co/api/people/2/')
let characterResponseJson = await characterResponse.json()
let films = await Promise.all(
  characterResponseJson.films.map(async filmUrl => {
    let filmResponse = await fetch(filmUrl)
    return filmResponse.json()
  })
)
console.log(films)
Comment

Promise.all() with async and await

const runAsyncFunctions = async () => {
  const users = await getUsers()

  Promise.all(
    users.map(async (user) => {
      const userId = await getIdFromUser(user)
      console.log(userId)

      const capitalizedId = await capitalizeIds(userId)
      console.log(capitalizedId)
    })
  )

  console.log(users)
}
Comment

Promise.all() with async and await to run in console

// First promise returns an array after a delay
const getUsers = () => {
  return new Promise((resolve, reject) => {
    return setTimeout(
      () => resolve([{ id: 'ranjeet' }, { id: 'adil' }, { id: 'preet' }]),
      600
    )
  })
}


// Second promise relies on the result of first promise
const getIdFromUser = (user) => {
  return new Promise((resolve, reject) => {
    return setTimeout(() => resolve(user.id), 500)
  })
}


// Third promise relies on the result of the second promise
const capitalizeIds = (id) => {
  return new Promise((resolve, reject) => {
    return setTimeout(() => resolve(id.toUpperCase()), 200)
  })
}

const runAsyncFunctions = async () => {
  const users = await getUsers()

  Promise.all(
    users.map(async (user) => {
      const userId = await getIdFromUser(user)
      console.log(userId)

      const capitalizedId = await capitalizeIds(userId)
      console.log(capitalizedId)
    })
  )

  console.log(users)
}

runAsyncFunctions()
Comment

PREVIOUS NEXT
Code Example
Javascript :: reactjs change fill color .svg 
Javascript :: node .env file example 
Javascript :: how to make an event listener only work once 
Javascript :: connect existing database with sequelize 
Javascript :: express controller 
Javascript :: mongoose bulk update 
Javascript :: javascript array loop 
Javascript :: pwa in angular 
Javascript :: dedecting invalid date in js 
Javascript :: parseint function javascript 
Javascript :: vs code file nesting 
Javascript :: javascript the event loop 
Javascript :: update to node 15.11 
Javascript :: NodeJS 10.24.1 
Javascript :: find how many similar object item in an array in javascript 
Javascript :: include hover in style jsx 
Javascript :: arrow function 
Javascript :: what is a for loop in javascript 
Javascript :: js string to arraybuffer 
Javascript :: json-server localhost 
Javascript :: javascript htmlcollection 
Javascript :: save sort order of jquery sortable 
Javascript :: look through object keys javascript 
Javascript :: mongoose create text index 
Javascript :: give a prop only if pass condition 
Javascript :: decorators in javascript 
Javascript :: use of this keyword in js 
Javascript :: d3.js 
Javascript :: horizontal scrollview in react js 
Javascript :: alternative to setinterval 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =