diff --git a/task-manager/playground/promise-chaining-2.js b/task-manager/playground/promise-chaining-2.js index 5cbbb48..4b6e4a5 100644 --- a/task-manager/playground/promise-chaining-2.js +++ b/task-manager/playground/promise-chaining-2.js @@ -1,11 +1,23 @@ require('../src/db/mongoose') const Task = require('../src/models/task') -Task.findByIdAndDelete('5e52f573b7d229017fe21321').then((task) => { - console.log(task) - return Task.countDocuments({ completed: false }) -}).then((result) => { - console.log(result) +// Task.findByIdAndDelete('5e52f573b7d229017fe21321').then((task) => { +// console.log(task) +// return Task.countDocuments({ completed: false }) +// }).then((result) => { +// console.log(result) +// }).catch((e) => { +// console.log(e) +// }) + +const deleteTaskAndCount = async (id) => { + const task = await Task.findByIdAndDelete(id) + const count = await Task.countDocuments({ completed: false }) + return count +} + +deleteTaskAndCount('5e489156e9232d006872395e').then((count) => { + console.log(count) }).catch((e) => { console.log(e) }) diff --git a/task-manager/playground/promise-chaining.js b/task-manager/playground/promise-chaining.js index 37c5b26..35003d2 100644 --- a/task-manager/playground/promise-chaining.js +++ b/task-manager/playground/promise-chaining.js @@ -1,13 +1,23 @@ 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) +// }) -User.findByIdAndUpdate('5e52f2612f736700e5a20797', { age: 1 }).then((user) => { - console.log(user) - return User.countDocuments({ age: 1 }) -}).then((result) => { - console.log(result) +const updateAgeAndCount = async (id, age) => { + const user = await User.findByIdAndUpdate(id, { age }) + const count = await User.countDocuments({ age }) + return count +} + +updateAgeAndCount('5e52f2612f736700e5a20797', 2).then((count) => { + console.log(count) }).catch((e) => { console.log(e) })