promise in javascript
promise,tipjs
const fs = require('fs')
const readFileWithPromise = file => {
return new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
}
readFileWithPromise('/etc/passwd')
.then(data => {
console.log(data.toString())
return readFileWithPromise('/etc/profile')
})
.then(data => {
console.log(data.toString())
})
.catch(err => {
console.log(err)
})
Có thể bạn đã miss một số snippets code
Hàm lày ngày trong tuần javascript
Hàm lày ngày trong tuần javascript
Copy a string to the clipboard
Copy a string to the clipboard. Only works as a result of user action (i.e. inside a click event listener).
Ajax, call jQuery POST to node.js expressjs
Ajax, call jQuery POST to node.js expressjs
Tránh viết biến có phạm vi global
Đôi khi bạn cần phải viết một tập lệnh trực tiếp trên trang. Cần lưu ý rằng trong code của bạn vô tình khai báo một biến toàn cầu. Vì phạm vi toàn cầu phức tạp hơn nên việc tìm kiếm chậm hơn rất nhiều
Promise.all là gì?
Promise.all là gì? Hiểu và sử dụng promise all trong những trường hợp nào trong vòng 1 phút.