diff --git a/playground/4-callbacks.js b/playground/4-callbacks.js index cf643df..bcb21f8 100644 --- a/playground/4-callbacks.js +++ b/playground/4-callbacks.js @@ -1,33 +1,14 @@ -// setTimeout(() => { -// 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) => { +const doWorkCallback = (callback) => { setTimeout(() => { - callback(x + y) + //callback('This is my error!', undefined) + callback(undefined, [1, 4, 7]) }, 2000) } -add(1, 4, (sum) => { - console.log(sum) // Should print: 5 +doWorkCallback((error, result) => { + if (error) { + return console.log(error) + } + + console.log(result) }) diff --git a/playground/8-promises.js b/playground/8-promises.js new file mode 100644 index 0000000..30d467e --- /dev/null +++ b/playground/8-promises.js @@ -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) +})