Video 93: Promise Chaining
This commit is contained in:
parent
b4f47db7ab
commit
6f0cc2fdaa
@ -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)
|
||||
})
|
||||
|
13
task-manager/playground/promise-chaining.js
Normal file
13
task-manager/playground/promise-chaining.js
Normal 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)
|
||||
})
|
@ -2,5 +2,6 @@ const mongoose = require('mongoose')
|
||||
|
||||
mongoose.connect('mongodb://localhost:27017/task-manager-api', {
|
||||
useNewUrlParser: true,
|
||||
useCreateIndex: true
|
||||
useCreateIndex: true,
|
||||
useFindAndModify: false
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user