CALLBACK

3665 ワード

CALLBACK

const printString = (string, callback) => {
   setTimeout(
     () => {
       console.log(string)
       callback()
     }, 
     Math.floor(Math.random() * 100) + 1
   )
 }

 const printAll = () => {
   printString("A", () => {
     printString("B", () => {
       printString("C", () => {})
     })
   })
 }
 printAll() A,B,C
コールバックによって順序を決定できます.