1
0

Video 93: Promise Chaining

This commit is contained in:
JayWll 2020-02-23 15:53:45 -07:00
parent b4f47db7ab
commit 6f0cc2fdaa
3 changed files with 41 additions and 11 deletions

View File

@ -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)
})

View File

@ -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)
})

View File

@ -2,5 +2,6 @@ const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/task-manager-api', {
useNewUrlParser: true,
useCreateIndex: true
useCreateIndex: true,
useFindAndModify: false
})