Page 67 - Nodejs 교과서 개정2판
P. 67
BTZOD BXBJU
֢٘ ߡࠗఠ ਗغח ӝמੑפ &4 ীࢲ ୶оغਵݴ ঌইفݶ ݈ ಞܻೠ ӝמੑפ ౠ ֢
٘ۢ ࠺زӝ ਤ۽ ۽Ӓې߁ਸ ೧ঠ ೡ ٸ ب ݆ ؾפ
۽झо ߔ ৠਸ ೧Ѿ೮݅ ৈ ٘о ടפ thenҗ catchо ҅ࣘ ߈ࠂغӝ ٸޙੑפ
async await ޙߨ ۽झܳ ࢎਊೠ ٘ܳ ೠ ߣ ؊ ӭՔೞѱ ੑפ
۽झ ٘ܳ द ೠ ߣ ࠁѷणפ
function findAndSaveUser(Users) {
Users.findOne({})
.then((user) => {
user.name = 'zero';
return user.save();
})
.then((user) => {
return Users.findOne({ gender: 'm' });
})
.then((user) => {
// 생략
})
.catch(err => {
console.error(err);
});
}
ߔҗ ܰѱ ٘ Өо Өয ঋ݅ ٘ח ৈ Ӥפ BTZOD await ޙߨਸ ࢎਊೞݶ җ э
߄Ե ࣻ णפ async functionۄח Ѫ ୶оغणפ
async function findAndSaveUser(Users) {
let user = await Users.findOne({});
user.name = 'zero';
user = await user.save();
user = await Users.findOne({ gender: 'm' });
// 생략
}
֥ۄ ب۽ ٘о ૣইणפ ೣࣻ ࢶࠗܳ ੌ߈ ೣࣻ न async functionਵ۽ Үೠ റ ۽झ