diff --git a/playground/8-promises.js b/playground/8-promises.js index 30d467e..63bbc59 100644 --- a/playground/8-promises.js +++ b/playground/8-promises.js @@ -1,12 +1,28 @@ -const doWorkPromise = new Promise((resolve, reject) => { - setTimeout(() => { - reject('Things went wrong!') - // resolve([7, 4, 1]) - }, 2000) -}) +const add = (a, b) => { + return new Promise((resolve, reject) => { + setTimeout(() => { + resolve(a + b) + }, 2000) + }) +} -doWorkPromise.then((result) => { - console.log('Success!', result) -}).catch((error) => { - console.log('Error!', error) +// add(1, 2).then((sum) => { +// console.log(sum) +// +// add(sum, 5).then((sum2) => { +// console.log(sum2) +// }).catch((e) => { +// console.log(e) +// }) +// }).catch((e) => { +// console.log(e) +// }) + +add(1, 1).then((sum) => { + console.log(sum) + return add(sum, 4) +}).then((sum2) => { + console.log(sum2) +}).catch((e) => { + console.log(e) }) diff --git a/task-manager/playground/promise-chaining.js b/task-manager/playground/promise-chaining.js new file mode 100644 index 0000000..37c5b26 --- /dev/null +++ b/task-manager/playground/promise-chaining.js @@ -0,0 +1,13 @@ +require('../src/db/mongoose') +const User = require('../src/models/user') + +// 5e519e3404357c00d0777aee + +User.findByIdAndUpdate('5e52f2612f736700e5a20797', { age: 1 }).then((user) => { + console.log(user) + return User.countDocuments({ age: 1 }) +}).then((result) => { + console.log(result) +}).catch((e) => { + console.log(e) +}) diff --git a/task-manager/src/db/mongoose.js b/task-manager/src/db/mongoose.js index e391751..a584075 100644 --- a/task-manager/src/db/mongoose.js +++ b/task-manager/src/db/mongoose.js @@ -2,5 +2,6 @@ const mongoose = require('mongoose') mongoose.connect('mongodb://localhost:27017/task-manager-api', { useNewUrlParser: true, - useCreateIndex: true + useCreateIndex: true, + useFindAndModify: false })