怎么写async await

暂且使用如下try/catch
In async/await functions it is common to use try/catch blocks to catch such errors.
`async function asyncTask(cb) {
try {
const user = await UserModel.findById(1);
if(!user) return cb('No user found');
} catch(e) {
return cb('Unexpected error occurred');
}

try {
   const savedTask = await TaskModel({userId: user.id, name: 'Demo Task'});
} catch(e) {
    return cb('Error occurred while saving task');
}

if(user.notificationsEnabled) {
    try {
        await NotificationService.sendNotification(user.id, 'Task Created');  
    } catch(e) {
        return cb('Error while sending notification');
    }
}

if(savedTask.assignedUser.id !== user.id) {
    try {
        await NotificationService.sendNotification(savedTask.assignedUser.id, 'Task was created for you');
    } catch(e) {
        return cb('Error while sending notification');
    }
}

cb(null, savedTask);

}`
参照
https://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/

上一篇:Python 异步编程 协程(async/await)


下一篇:对于Linq查询关键字及await,async异步关键字的扩展使用