March 20th, 2019
A promise is an object that may produce a single value some time in the future
promise๋ javascript ๋น๋๊ธฐ ์ฒ๋ฆฌ์ ์ฌ์ฉ๋๋ ๊ฐ์ฒด์ด๋ค.
promise ์ค์ต์ ์ํด ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
const add = (a, b) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(a+b)
}, 2000)
})
}
add(1, 2)
.then((sum) => {
console.log(sum)
add(sum, 5)
.then((sum2)=> {
console.log(sum2)
})
.catch(error1 => {
console.log(error1)
})
})
.catch(error => {
console.log(error)
})
Promise chaining์ ํตํด ๋ ๊ฐ๊ฒฐํ ์ฝ๋๋ฅผ ๋ณผ ์์๋ค.
add(1, 1)
.then(sum => {
console.log(sum)
return add(sum, 4)
})
.then(sum2 => {
console.log(sum2)
})
.catch(error => {
console.log(error)
})
Promise Chaining์ ์ด์ฉํ update
require('../src/db/mongoose')
const User = require('../src/models/user')
User.findByIdAndUpdate('5c7b8e0ea401383fea0d8084', { age: 12 })
.then(user => {
console.log(user)
return User.countDocuments({ age: 12 })
})
.then(result => {
console.log(result)
})
.catch(error => {
console.log(error)
})
์ ์์ ์ผ๋ก ์๋ํ์ง๋ง,
DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.
์ด๋ฌํ ๊ฒฝ๊ณ ๊ฐ ๋ฐ์ํ๋๋ฐ
mongoose.connect('mongodb://127.0.0.1:27017/task-manager', {
auth: {
"authSource": "admin"
},
user: 'user',
password: 'passoword',
useNewUrlParser: true,
useFindAndModify: false,
})
์์ ๊ฐ์ด useFindAndModify ์ค์ ์ false๋ก ๋ณ๊ฒฝํ๋ฉด ๋๋ค.
๋ํ, findByIdAndUpdate() method๋ update ์ด์ ์ ์๋ณธ document๋ฅผ return ํ๋ค.
task ๋ก ์ค์ตํด๋ณธ promise chaining
require('../src/db/mongoose')
const Task = require('../src/models/task')
Task.findByIdAndDelete('5c7b9c3a6e27c7bca66049ac')
.then(task => {
console.log(task + ' is deleted')
return Task.countDocuments({ completed: false })
})
.then(numOfTasks => {
console.log(numOfTasks + ' tasks are not completed')
})
.catch(error => {
console.log(error)
})
๊ฒฐ๊ณผ๊ฐ
{ completed: true,
_id: 5c7b9c3a6e27c7bca66049ac,
description: '๋ธ๋ก๊ทธํฌ์คํ
ํ๊ธฐ' } is deleted
11 tasks are not completed
test 1
const doWork = () => {
}
console.log(doWork())
// <result>
// undefined
test 2
const doWork = async () => {
}
console.log(doWork())
// <result>
// Promise { undefined }
Async์ ์ํด Promise ๊ฐ์ฒด๊ฐ ๋ฐํ๋์๋ค.
test 3
const doWork = async () => {
return 'Nailer'
}
console.log(doWork())
// <result>
// Promise { 'Nailer' }
๋ฐ๋ผ์ ํญ์ async๋ Promise ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค๊ณ ๋ณผ ์ ์๋ค.
test 4
const doWork = async () => {
return 'Nailer'
}
doWork()
.then(result => {
console.log('result', result)
})
.catch(error => {
console.log('error', error)
})
// <result>
// result Nailer
Promise ๊ฐ์ฒด์ด๋ฏ๋ก ์์ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
test 5
const add = (a, b) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(a + b)
}, 2000)
})
}
const doWork = async () => {
const sum = await add(1, 99)
const sum2 = await add(sum, 50)
const sum3 = await add(sum2, 3)
return sum3
}
doWork()
.then(result => {
console.log('result', result)
})
.catch(error => {
console.log('error', error)
})
test 6
const add = (a, b) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(a < 0 || b < 0) {
return reject('numbers must be non-negative')
}
resolve(a + b)
}, 2000)
})
}
const doWork = async () => {
const sum = await add(1, 99)
const sum2 = await add(sum, 50)
const sum3 = await add(sum2, -3)
return sum3
}
doWork()
.then(result => {
console.log('result', result)
})
.catch(error => {
console.log('error', error)
})
// <result> 6์ด ๋ค...
// error numbers must be non-negative
const doWork = async () => {
const sum = await add(-1, 99)
const sum2 = await add(sum, 50)
const sum3 = await add(sum2, -3)
return sum3
}
// <result> 2์ด ๋ค...
// error numbers must be non-negative
์ฒซ ๋ฒ์งธ await์์ error shooting์ ํด์ ๋ค์ await๋ค์ ์คํ๋์ง ์๋๋ค.
๋ฐฐ์ด Promise์ async/await ์ฝ๋๋ฅผ ํผ์ฉํ ๊ฒฝ์ฐ
require('../src/db/mongoose')
const User = require('../src/models/user')
User.findByIdAndUpdate('5c7b8e0ea401383fea0d8084', { age: 111 })
.then(user => {
console.log(user)
return User.countDocuments({ age: 111 })
})
.then(result => {
console.log(result, 'PromiseChaining')
})
.catch(error => {
console.log(error)
})
const updateAgeAndCount = async (id, age) => {
const user = await User.findByIdAndUpdate(id, { age: age })
const count = await User.countDocuments({age: age})
return count
}
updateAgeAndCount('5c7b8e0ea401383fea0d8084', 3)
.then(count => {
console.log(count, 'async/await')
})
.catch(error => {
console.log(error)
})
์ด์ ๊ฐ์ด ๊ฐ๋ณ์ ์ผ๋ก ํ๋์ document์ ๋ํ ์์ ์ ๊ฐํ์ ๋
updateAgeAndCount๊ฐ ๋จผ์
User.findByIdAndUpdate('5c7b8e0ea401383fea0d8084', { age: 111 }) //The operation returns the original document before the update:
.then(user => {
console.log(user)
return User.countDocuments({ age: 111 })
})
.then(result => {
console.log(result, 'PromiseChaining')
return updateAgeAndCount('5c7b8e0ea401383fea0d8084', 3)
})
.then(count => {
console.log(count, 'async/await')
})
.catch(error => {
console.log(error)
})
์ด์ ๊ฐ์ด Chaining์ ํ์ฉํ๋ค.
Promise์ async/await๋ฅผ ์ฌ์ฉํด์์ง๋ง, ๋ช ํํ๊ฒ ์ ์ ์๋ ๊ธฐํ๋ฅผ ๊ฐ์ก๋ค.