Page 68 - Nodejs 교과서 개정2판
P. 68
খী awaitਸ ࠢणפ ઁ ೣࣻח ೧ ۽झо resolveؼ ٸө ӝܽ ٍ ۽ਵ۽ ֈযцפ
ܳ ٜݶ await Users.findOne({}) resolveؼ ٸө ӝܽ ী user ߸ࣻܳ ୡӝചೞח Ѫੑפ
ਤ ٘ח ী۞ܳ ܻೞח ࠗ࠙ ۽झо rejectػ ҃ হਵ۽ җ э ୶о স ਃפ
async function findAndSaveUser(Users) {
try {
let user = await Users.findOne({});
user.name = 'zero';
user = await user.save();
user = await Users.findOne({ gender: 'm' });
// 생략
} catch (error) {
console.error(error);
}
}
try/catchޙਵ۽ ۽ਸ хसणפ ۽झ catch ݫࢲ٘ۢ try/catchޙ catchо ী۞ܳ ܻפ
ച ೣࣻب BTZOD৬ э ࢎਊೡ ࣻ णפ
const findAndSaveUser = async (Users) => {
try {
let user = await Users.findOne({});
user.name = 'zero';
user = await user.save();
user = await Users.findOne({ gender: 'm' });
// 생략
} catch (error) {
console.error(error);
}
};.
forޙҗ async awaitਸ э ॄࢲ ۽झܳ ࣽରਵ۽ प೯ೡ ࣻ णפ forޙҗ ೣԋ ॳח Ѫ ֢٘
ߡࠗఠ ਗೞח &4 ޙߨੑפ
const promise1 = Promise.resolve('성공1');
const promise2 = Promise.resolve('성공2');