Advertisement
thesuhu

Nodejs Promise

Dec 27th, 2020 (edited)
1,229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # The return of the .then function will be the resolved value of the promise.
  2. # tambahkan await sebelum initPromise jika mau menunggu proses
  3. function initPromise() {
  4.   return new Promise(function(res, rej) {
  5.     res("initResolve")
  6.   })
  7. }
  8.  
  9. initPromise()
  10.   .then(function(result) {
  11.     console.log(result) // "initResolve"
  12.     return "normalReturn"
  13.   })
  14.   .then(function(result) {
  15.     console.log(result) // "normalReturn"
  16.   })
  17.  
  18. # If the .then function returns a Promise, then the resolved value of that chained promise is passed to the following .then.
  19. function initPromise() {
  20.   return new Promise(function(res, rej) {
  21.     res("initResolve")
  22.   })
  23. }
  24.  
  25. initPromise()
  26.   .then(function(result) {
  27.     console.log(result) // "initResolve"
  28.     return new Promise(function(resolve, reject) {
  29.        setTimeout(function() {
  30.           resolve("secondPromise")
  31.        }, 1000)
  32.     })
  33.   })
  34.   .then(function(result) {
  35.     console.log(result) // "secondPromise"
  36.   })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement