标签:saving === 参考 mis rip sync fun eject 代码量
function asyncTask(cb) {
asyncFuncA.then(AsyncFuncB)
.then(AsyncFuncC)
.then(AsyncFuncD)
.then(data => cb(null, data)
.catch(err => cb(err));
}
async function asyncTask(cb) {
const user = await UserModel.findById(1);
if(!user) return cb(‘No user found‘);
const savedTask = await TaskModel({userId: user.id, name: ‘Demo Task‘});
if(user.notificationsEnabled) {
await NotificationService.sendNotification(user.id, ‘Task Created‘);
}
if(savedTask.assignedUser.id !== user.id) {
await NotificationService.sendNotification(savedTask.assignedUser.id, ‘Task was created for you‘);
}
cb(null, savedTask);
}
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);
}
export default function to(promise) {
return promise.then(data => {
return [null, data];
})
.catch(err => [err]);
}
function taskPromise(status) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (status === "fail") {
return reject("error")
} else {
return resolve("success")
}
}, 1000)
})
}
async function asyncTasks() {
let err, result
[err, result] = await to(taskPromise(""))
if (err) {
console.log("it‘s error")
} else {
console.log("it‘s" + result)
}
[err, result] = await to(taskPromise("fail"))
if (err) {
console.log("it‘s error")
} else {
console.log("it‘s" + result)
}
}
asyncTasks() //it‘ssuccess it‘s error
标签:saving === 参考 mis rip sync fun eject 代码量
原文地址:https://www.cnblogs.com/chrissong/p/10841760.html