Advertisement
langbung01

promise

Sep 4th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const promise = new Promise((res,rej)=>{
  2.     if(true)
  3.         res("it's pass");
  4.     else
  5.         rej("there re error!!!");
  6. })
  7.  
  8. promise
  9.     .then(res=>res+"!!!")
  10.     .then(res1=>{
  11.         console.log(res1);
  12.     })
  13.     .catch(console.log("error for then 1 and 2"))
  14.     .then(res3=>{
  15.         throw Error;
  16.         console.log(res3);
  17.     })
  18.     .catch(()=>console.log("error for then 3"))
  19.  
  20. const promise1 = new Promise(
  21.     (res,rej) => {
  22.         setTimeout(res,100,"first")
  23.     }
  24. );
  25.  
  26. const promise2 = new Promise(
  27.     (res,rej) => {
  28.         setTimeout(res,1000,"SECOND")
  29.     }
  30. );
  31.  
  32. const promise3 = new Promise(
  33.     (res,rej) => {
  34.         setTimeout(res,2000,"THIRD")
  35.     }
  36. );
  37.  
  38. Promise.all([promise,promise1,promise2,promise3])
  39.     .then((res)=> console.log(res))
  40.     .catch((err)=>console.log(err));
  41.  
  42. const url = [
  43.     "https://jsonplaceholder.typicode.com/posts",
  44.     "https://jsonplaceholder.typicode.com/users",
  45.     "https://jsonplaceholder.typicode.com/albums"
  46. ]
  47.  
  48. Promise.all(
  49.     url.map(url=>{  
  50.         return fetch(url).then(res=>res.json());
  51.     })
  52. )
  53. .then(
  54.     res=>{
  55.         console.log(res[0]);
  56.         console.log(res[1]);
  57.         console.log(res[2])
  58.     }
  59. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement