You are on page 1of 2

JavaScript ES6 cheat sheet ติดตามอัพเดท JavaScript Cheat Sheet เวอร์ชันใหม่ได้ที่นี่ >

ใช้งาน Promise
Variable function nameCat (name = 'George') {
return `My ${name} is so cute!`
Destructuring myTask.then(() => {
}
การประกาศตัวแปร console.log(nameCat()) การประกาศค่าผ่าน Array // ถ้า resolve (รันแล้วสามารถทำงานได้ตามเงื่อนไข)
จะทำงานใน then
var a การประกาศตัวแปรปกติใน JS แต่ Rest arguments const [name, age] = ['James', '3'] console.log('success yeah!')
มีปัญหาคือทะลุ block scope function countNum(...num) { console.log(`name: ${name}`)
let x การประกาศตัวแปรใหม่ใน ES6 ที่ return`Count: ${num.length}` console.log(`age: ${age}`) }).catch(() => {
เป็นตัวแปรที่ไม่ทะลุ block scope แล้ว }

การประกาศค่าผ่าน Object (ชื่อ


const PI ประกาศตัวแปรแบบ ค่าคงที่ console.log(countNum(5,6,7,8,1,2,3,4)) // ถ้า reject (รันแล้วไม่สามารถทำงานได้ตามเงื่อนไข)
จะทำงานใน catch
Spread property ต้องตรงกับชื่อตัวแปร) console.log('error T^T')
console.log(countNum(...[5,6,7,8,1,2,3,4]))
String //มีค่าเหมือน console.log(countNum(5,6,7,8,1,2,3,4)) let {name, age} = {name:'James', age:'3'}
}).finally(() => {
console.log(`name: ${name}`)

Backtick : เครื่องหมายขีดเฉียงที่ปุ่มตัวหนอน `
console.log(`age: ${age}`)
// finally จะทำงานหลังสุดทุกกรณี ไม่ว่าจะทำ then หรือ

ประกาศ string หลายบรรทัดแบบไม่ต้องแยกตัวมาบวกกัน


Arrow function การวน Property ใน Object ใน Array
catch มาก่อนก็ตาม
console.log('bye bye.')
let myString = `Hello
let cats = [{name:'James', age:'3'},
from the แบบมาตรฐาน {name:'Sali', age:'1'}]
})
other side`
แบบก่อนธรรมดา for (let {name, age} of cats) {
จัดรูปแบบ string function myFunc(a, b, c) { console.log(`name: ${name}`) Promise.all()
let name = 'Pompam' return a + b + c console.log(`age: ${age}`)
console.log(`==============`) สร้าง Promise เพื่อจัดการ Promise หลายๆตัวมารวม
console.log(`Hello ${name}`) }
} เป็นตัวเดียว
console.log(myFunc(1, 2, 3))
//output
includes() : เช็คว่ามีคำใน string หรือไม่ แบบ Arrow name: James let myTask1 = new Promise((resolve, reject) => {...})
age: 3 let myTask2 = new Promise((resolve, reject) => {...})
var myFunc = (a, b, c) => {
console.log('My name is Pompam'.includes('Pom')) ============== let myTask3 = new Promise((resolve, reject) => {...})
return a + b + c
console.log('My name is Pompam'.includes('Noo') ... let myTask4 = new Promise((resolve, reject) => {...})
}
let myTask5 = new Promise((resolve, reject) => {...})
console.log(myFunc(1, 2, 3))
startsWith() : เช็คว่า string ขึ้นต้นด้วยคำที่
.

ต้องการหรือไม่ แบบมี Parameter ตัวเดียว Promise .


let myTask(n) = new Promise((resolve, reject) => {...})

console.log('My name is Pompam'.startsWith('name')) ถ้ามีตัวเดียวจะใส่หรือไม่ใส่ ( ) ครอบก็ได้ สร้าง Promise Promise.all([myTask1, myTask2, myTask3, myTask4,
console.log('My name is Pompam'.startsWith('My')) var myFunc = a => { myTask5, ..., myTask(n)]).then(() =>
return a * 100 let result = 'success'
// สร้าง Promise
repeat() - รีเทิร์น string ซ้ำตามจำนวนที่กำหนด
} // Promise ทุกตัว resolve
console.log(myFunc(5)) let myTask = new Promise((resolve, reject) => {
console.log('All task have been completed.')
// โค้ดที่ทำงานแบบ Asynchronous
console.log('My'.repeat(5)) if (result === 'success') {
แบบ Return ทันที // กรณีทำงานเสร็จส่งผลลัพธ์ด้วย resolve()
).catch(() =>

resolve('ok')
ถ้าไม่ใส่ { } จะ return ค่าที่ได้โดยอัตโนมัติ // มีตัวที่ reject
Function argument var myFunc = (a, b, c) => a + b + c
}
else {
console.log('Some task failed.')
console.log(myFunc(5, 6, 7)) )
// กรณี error ส่งด้วย reject()
Default arguments สร้างค่าตังต้้ นให้กับ function reject('error')
**หากตัวใดตัว reject จะไม่เข้าเงื่อนไข then ทุกกรณี**
หากไม่ใส่ Argument ลงไปในการเรียก function ค่าของ }
Argument ที่ใส่เข้าไปจะเป็นค่าที่เราประกาศไว้ })

COPYRIGHT©2020 BorntoDev ALL RIGHTS RESERVED


JavaScript ES6 cheat sheet (Easy print version) ติดตามอัพเดท JavaScript Cheat Sheet เวอร์ชันใหม่ได้ที่นี่ >

ใช้งาน Promise
Variable function nameCat (name = 'George') {
return `My ${name} is so cute!`
Destructuring myTask.then(() => {
}
การประกาศตัวแปร console.log(nameCat()) การประกาศค่าผ่าน Array // ถ้า resolve (รันแล้วสามารถทำงานได้ตามเงื่อนไข)
จะทำงานใน then
var a การประกาศตัวแปรปกติใน JS แต่ Rest arguments const [name, age] = ['James', '3'] console.log('success yeah!')
มีปัญหาคือทะลุ block scope function countNum(...num) { console.log(`name: ${name}`)
let x การประกาศตัวแปรใหม่ใน ES6 ที่ return`Count: ${num.length}` console.log(`age: ${age}`) }).catch(() => {
เป็นตัวแปรที่ไม่ทะลุ block scope แล้ว }

การประกาศค่าผ่าน Object (ชื่อ


const PI ประกาศตัวแปรแบบ ค่าคงที่ console.log(countNum(5,6,7,8,1,2,3,4)) // ถ้า reject (รันแล้วไม่สามารถทำงานได้ตามเงื่อนไข)
จะทำงานใน catch
Spread property ต้องตรงกับชื่อตัวแปร) console.log('error T^T')
console.log(countNum(...[5,6,7,8,1,2,3,4]))
String //มีค่าเหมือน console.log(countNum(5,6,7,8,1,2,3,4)) let {name, age} = {name:'James', age:'3'}
}).finally(() => {
console.log(`name: ${name}`)

Backtick : เครื่องหมายขีดเฉียงที่ปุ่มตัวหนอน `
console.log(`age: ${age}`)
// finally จะทำงานหลังสุดทุกกรณี ไม่ว่าจะทำ then หรือ

ประกาศ string หลายบรรทัดแบบไม่ต้องแยกตัวมาบวกกัน


Arrow function การวน Property ใน Object ใน Array
catch มาก่อนก็ตาม
console.log('bye bye.')
let myString = `Hello
let cats = [{name:'James', age:'3'},
from the แบบมาตรฐาน {name:'Sali', age:'1'}]
})
other side`
แบบก่อนธรรมดา for (let {name, age} of cats) {
จัดรูปแบบ string function myFunc(a, b, c) { console.log(`name: ${name}`) Promise.all()
let name = 'Pompam' return a + b + c console.log(`age: ${age}`)
console.log(`==============`) สร้าง Promise เพื่อจัดการ Promise หลายๆตัวมารวม
console.log(`Hello ${name}`) }
} เป็นตัวเดียว
console.log(myFunc(1, 2, 3))
//output
includes() : เช็คว่ามีคำใน string หรือไม่ แบบ Arrow name: James let myTask1 = new Promise((resolve, reject) => {...})
age: 3 let myTask2 = new Promise((resolve, reject) => {...})
var myFunc = (a, b, c) => {
console.log('My name is Pompam'.includes('Pom')) ============== let myTask3 = new Promise((resolve, reject) => {...})
return a + b + c
console.log('My name is Pompam'.includes('Noo') ... let myTask4 = new Promise((resolve, reject) => {...})
}
let myTask5 = new Promise((resolve, reject) => {...})
console.log(myFunc(1, 2, 3))
startsWith() : เช็คว่า string ขึ้นต้นด้วยคำที่
.

ต้องการหรือไม่ แบบมี Parameter ตัวเดียว Promise .


let myTask(n) = new Promise((resolve, reject) => {...})

console.log('My name is Pompam'.startsWith('name')) ถ้ามีตัวเดียวจะใส่หรือไม่ใส่ ( ) ครอบก็ได้ สร้าง Promise Promise.all([myTask1, myTask2, myTask3, myTask4,
console.log('My name is Pompam'.startsWith('My')) var myFunc = a => { myTask5, ..., myTask(n)]).then(() =>
return a * 100 let result = 'success'
// สร้าง Promise
repeat() - รีเทิร์น string ซ้ำตามจำนวนที่กำหนด
} // Promise ทุกตัว resolve
console.log(myFunc(5)) let myTask = new Promise((resolve, reject) => {
console.log('All task have been completed.')
// โค้ดที่ทำงานแบบ Asynchronous
console.log('My'.repeat(5)) if (result === 'success') {
แบบ Return ทันที // กรณีทำงานเสร็จส่งผลลัพธ์ด้วย resolve()
).catch(() =>

resolve('ok')
ถ้าไม่ใส่ { } จะ return ค่าที่ได้โดยอัตโนมัติ // มีตัวที่ reject
Function argument var myFunc = (a, b, c) => a + b + c
}
else {
console.log('Some task failed.')
console.log(myFunc(5, 6, 7)) )
// กรณี error ส่งด้วย reject()
Default arguments สร้างค่าตังต้้ นให้กับ function reject('error')
**หากตัวใดตัว reject จะไม่เข้าเงื่อนไข then ทุกกรณี**
หากไม่ใส่ Argument ลงไปในการเรียก function ค่าของ }
Argument ที่ใส่เข้าไปจะเป็นค่าที่เราประกาศไว้ })

COPYRIGHT©2020 BorntoDev ALL RIGHTS RESERVED

You might also like