1
0

Video 79: Promises

This commit is contained in:
JayWll 2020-02-15 11:59:45 -07:00
parent e4662eb729
commit c0feae36d5
2 changed files with 21 additions and 28 deletions

View File

@ -1,33 +1,14 @@
// setTimeout(() => { const doWorkCallback = (callback) => {
// console.log('Two seconds are up')
// }, 2000)
//
// const names = ['Jason', 'Jen', 'Jess']
// const shortNames = names.filter((name) => {
// return name.length <= 4
// })
//
// const geocode = (address, callback) => {
// setTimeout(() => {
// const data = {
// latitude: 0,
// longitude: 0
// }
//
// callback(data)
// }, 2000)
// }
//
// geocode('Philadelphia', (data) => {
// console.log(data)
// })
const add = (x, y, callback) => {
setTimeout(() => { setTimeout(() => {
callback(x + y) //callback('This is my error!', undefined)
callback(undefined, [1, 4, 7])
}, 2000) }, 2000)
} }
add(1, 4, (sum) => { doWorkCallback((error, result) => {
console.log(sum) // Should print: 5 if (error) {
return console.log(error)
}
console.log(result)
}) })

12
playground/8-promises.js Normal file
View File

@ -0,0 +1,12 @@
const doWorkPromise = new Promise((resolve, reject) => {
setTimeout(() => {
reject('Things went wrong!')
// resolve([7, 4, 1])
}, 2000)
})
doWorkPromise.then((result) => {
console.log('Success!', result)
}).catch((error) => {
console.log('Error!', error)
})