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 :: what is synchronous and asynchronous in javascript 
Javascript :: json update pytohn 
Javascript :: how to sort json objects 
Javascript :: js string to json 
Javascript :: jquery insert after next element 
Javascript :: react native paper text input 
Javascript :: json example 
Javascript :: js host without port 
Javascript :: append element to specific class 
Javascript :: how to comment in a json file 
Javascript :: javascript example of foreach loop 
Javascript :: javascript arithmetic operators 
Javascript :: import img react in another file 
Javascript :: xmlhttprequest 
Javascript :: js create object from array 
Javascript :: encrypt decrypt javascript 
Javascript :: regex search for all math operators 
Javascript :: displaying the date react 
Javascript :: js filter to remove empty string in array. 
Javascript :: javascript get phone number from string 
Javascript :: for of js 
Javascript :: axios how to get error response 
Javascript :: upload files to api using axios 
Javascript :: timestamp convert moment vue 
Javascript :: react tostify 
Javascript :: dynamodb pagination nodejs 
Javascript :: .toJSON() in sequelize 
Javascript :: kendo template multiselect default selected 
Javascript :: jquery change query string parameter value 
Javascript :: loop through json array and get key name 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =