Javascript 将回调更改为承诺链
我正在尝试开始使用承诺链接,到目前为止,我正在使用回调,我想编辑以下代码:Javascript 将回调更改为承诺链,javascript,node.js,asynchronous,callback,promise,Javascript,Node.js,Asynchronous,Callback,Promise,我正在尝试开始使用承诺链接,到目前为止,我正在使用回调,我想编辑以下代码: Account.findById(req.user._id, (err, acc) => { if (err) console.log(err); var r = req.body; acc.fullName = r.fullName; acc.displayname = r.username; acc.city = r.cit
Account.findById(req.user._id,
(err, acc) => {
if (err) console.log(err);
var r = req.body;
acc.fullName = r.fullName;
acc.displayname = r.username;
acc.city = r.city;
acc.province = r.province;
acc.postalCode = r.postalCode;
acc.phone = r.phone;
acc.ageGroup = r.ageGroup;
acc.education = r.education;
acc.lookingForWork = r.lookingForWork;
acc.employmentStatus = r.employmentStatus;
acc.workingWithEOESC = r.workingWithEOESC;
acc.resume = r.resume;
acc.mainWorkExp = r.mainWorkExp;
acc.save();
res.redirect('/seeker');
})
这就是我试图做的:
Account.findById(req.user._id)
.then((err, acc) => {
if (err) console.log(err);
var r = req.body;
acc.fullName = r.fullName;
acc.displayname = r.username;
acc.city = r.city;
acc.province = r.province;
acc.postalCode = r.postalCode;
acc.phone = r.phone;
acc.ageGroup = r.ageGroup;
acc.education = r.education;
acc.lookingForWork = r.lookingForWork;
acc.employmentStatus = r.employmentStatus;
acc.workingWithEOESC = r.workingWithEOESC;
acc.resume = r.resume;
acc.mainWorkExp = r.mainWorkExp;
acc.save();
})
.catch(e => console.log(e))
.then((acc) => {
console.log(acc);
res.redirect('/seeker');
})
});
但是promise版本抛出一个TypeError:无法设置未定义错误的属性“fullName”
未保存更改,控制台登录acc会导致未定义。忘了在帖子里加上那个 我只是在学习承诺。我错过了什么?内部代码几乎完全相同。然后在成功时调用,因此绝对没有错误:
then((acc) => {
然后在成功时调用,因此绝对没有错误:
then((acc) => {
。然后,Promission中的函数可以接受最大两个参数,这两个参数必须都是函数,第一个函数是当Promission已满时,第二个函数是当Promission被拒绝时,或者您可以只将一个函数传递给。然后,使用.catch来处理任何类型的错误或被拒绝的承诺
var f1 = acc => console.log(acc); // logs out the acc object;
var f2 = err => console.log(err); // logs out error while executing the promise
.then(f1,f2); // when you do this there is no need for a catch block
// or
.then( acc => {
console.log(acc) // logs out the acc object
}).catch( err => console.log(err) ) //logs out the error
// if you need to handle another value
.then( acc => {
console.log(acc);
return acc.save(); //lets say acc.save() returns an object
}).then( acc => console.log(acc) ); // the value of acc.save() is passed down to the next `.then` block
。然后,Promission中的函数可以接受最大两个参数,这两个参数必须都是函数,第一个函数是当Promission已满时,第二个函数是当Promission被拒绝时,或者您可以只将一个函数传递给。然后,使用.catch来处理任何类型的错误或被拒绝的承诺
var f1 = acc => console.log(acc); // logs out the acc object;
var f2 = err => console.log(err); // logs out error while executing the promise
.then(f1,f2); // when you do this there is no need for a catch block
// or
.then( acc => {
console.log(acc) // logs out the acc object
}).catch( err => console.log(err) ) //logs out the error
// if you need to handle another value
.then( acc => {
console.log(acc);
return acc.save(); //lets say acc.save() returns an object
}).then( acc => console.log(acc) ); // the value of acc.save() is passed down to the next `.then` block
之所以发生这种情况,是因为函数“findById”可能没有返回“promise”,只是返回了一些响应。您需要在findById函数中创建一个“promise对象”并返回它
findById (){
let promise = new Promise((resolve, reject) => {
Suppose results is yield from some async task, so when
//wanted results occured
resolve(value);
//unwanted result occured
reject(new Error('Something happened!'));
return promise;
}
findById.then(response => {
console.log(response);
}, error => {
console.log(error);
});
之所以发生这种情况,是因为函数“findById”可能没有返回“promise”,只是返回了一些响应。您需要在findById函数中创建一个“promise对象”并返回它
findById (){
let promise = new Promise((resolve, reject) => {
Suppose results is yield from some async task, so when
//wanted results occured
resolve(value);
//unwanted result occured
reject(new Error('Something happened!'));
return promise;
}
findById.then(response => {
console.log(response);
}, error => {
console.log(error);
});
基于回调的API有一个共同的约定,即使用回调函数的第一个参数来指示失败。承诺不需要这样的约定,因为它们有处理失败的内置方法,所以您只需要操作第一个参数,而不是第二个参数。第二个参数将是未定义的,这将导致您看到的错误 大多数情况下,当您将基于回调的代码转换为基于承诺的代码时,您希望使用以下模式作为基本指南:
// Callback-based:
asyncFn((err, result) => {
if (err) {
// handle failure
} else {
// handle success
}
});
// Promise-based equivalent:
asyncFnPromise()
.then((result) => {
// handle success
}, (err) => {
// handle failure
});
// Alternative promised-based:
asyncFnPromise()
.then((result) => {
// handle success.
// Note that unlike the above, any errors thrown here will trigger
// the `catch` handler below, in addition to actual asyncFnPromise
// failures.
})
.catch((err) => {
// handle failure
});
基于回调的API有一个共同的约定,即使用回调函数的第一个参数来指示失败。承诺不需要这样的约定,因为它们有处理失败的内置方法,所以您只需要操作第一个参数,而不是第二个参数。第二个参数将是未定义的,这将导致您看到的错误 大多数情况下,当您将基于回调的代码转换为基于承诺的代码时,您希望使用以下模式作为基本指南:
// Callback-based:
asyncFn((err, result) => {
if (err) {
// handle failure
} else {
// handle success
}
});
// Promise-based equivalent:
asyncFnPromise()
.then((result) => {
// handle success
}, (err) => {
// handle failure
});
// Alternative promised-based:
asyncFnPromise()
.then((result) => {
// handle success.
// Note that unlike the above, any errors thrown here will trigger
// the `catch` handler below, in addition to actual asyncFnPromise
// failures.
})
.catch((err) => {
// handle failure
});
未保存更改,控制台登录acc会导致未定义。忘了把它加在表格里了post@alex3wielki也许帐户还不存在?代码在同一个地方。帐户在顶部声明更改未保存,控制台登录acc导致未定义。忘了把它加在表格里了post@alex3wielki也许帐户还不存在?代码在同一个地方。帐户在顶部声明这与我的非工作代码有何不同?我只是想在你尝试的基于承诺的代码的第二行找到我的错误OK。在提供给的函数中有两个参数。您应该只有一个。请从该函数中删除错误检查内容。错误检查应该在一个单独的函数中处理,无论是在第二个示例的第二个参数中,还是在第三个示例的catch中,这与我的非工作代码有何不同?我只是想在你尝试的基于承诺的代码的第二行找到我的错误OK。在提供给的函数中有两个参数。您应该只有一个。请从该函数中删除错误检查内容。错误检查应该在单独的函数中处理,或者在第二个示例的第二个参数中,或者在第三个示例的catch中