我如何抽象一个 Javascript Promise 并保留 .then
How do I abstract a Javascript Promise and retain the .then
我有一个名为 sweetAlertConfirmationMessage
的函数。它接受一条消息,将其显示为文本,然后等待用户确认或取消弹出警报。
function sweetAlertConfirmationMessage(message) {
Swal.fire({
title: T('SWEETALERT_GENERAL_CONFIRMATION_TITLE'),
text: message,
icon: 'warning',
showCancelButton: true,
confirmButtonColor: T('SWEETALERT_CONFIRM_BUTTON_COLOR'),
cancelButtonColor: T('SWEETALERT_CANCEL_BUTTON_COLOR'),
cancelButtonText: T('SWEETALERT_CANCEL_BUTTON_TEXT'),
confirmButtonText: T('SWEETALERT_CONFIRM_BUTTON_TEXT'),
})
.then(result => {
return new Promise(function (resolve, reject) {
resolve(true);
});
})
.catch(result => {
console.log(result);
});
}
我想要实现的是:
sweetAlertConfirmationMessage(T('SWEETALERT_MESSAGE_DELETE_PRODUCT_FROM_CART'))
.then(
// Run code here.
)
但是,我收到以下错误:
Uncaught (in promise) TypeError: Cannot read property 'then' of undefined
这是我观察到的情况:
- sweetAlertConfirmationMessage 方法是运行。
- 在 promise 被 resolve 之前,代码继续离开函数,进入 .then(// 运行 code here)
- .then 不是 运行,因为它没有对
采取行动的承诺
- 显示通知消息,等待用户确认或取消。
console.log(结果)不显示任何内容。
我研究了以下主题:
- 异步并等待
- 承诺、.then 和 .catch
- 承诺链接
如何按所需顺序执行此代码:
- 显示确认信息
- 等待承诺:如果确认则为 true,如果单击其他任何内容则为 undefined
- 运行 .then(//运行 代码)
我希望这个答案写得很好,可以在以后帮助到其他人。
function sweetAlertConfirmationMessage(message) {
return Swal.fire({
title: T('SWEETALERT_GENERAL_CONFIRMATION_TITLE'),
text: message,
icon: 'warning',
showCancelButton: true,
confirmButtonColor: T('SWEETALERT_CONFIRM_BUTTON_COLOR'),
cancelButtonColor: T('SWEETALERT_CANCEL_BUTTON_COLOR'),
cancelButtonText: T('SWEETALERT_CANCEL_BUTTON_TEXT'),
confirmButtonText: T('SWEETALERT_CONFIRM_BUTTON_TEXT'),
})
}
只是 return 您函数的承诺。您可以这样使用:
sweetAlertConfirmationMessage('msg').then(x => {})
阅读有关 Promisechaining 的更多信息:
https://javascript.info/promise-chaining
我有一个名为 sweetAlertConfirmationMessage
的函数。它接受一条消息,将其显示为文本,然后等待用户确认或取消弹出警报。
function sweetAlertConfirmationMessage(message) {
Swal.fire({
title: T('SWEETALERT_GENERAL_CONFIRMATION_TITLE'),
text: message,
icon: 'warning',
showCancelButton: true,
confirmButtonColor: T('SWEETALERT_CONFIRM_BUTTON_COLOR'),
cancelButtonColor: T('SWEETALERT_CANCEL_BUTTON_COLOR'),
cancelButtonText: T('SWEETALERT_CANCEL_BUTTON_TEXT'),
confirmButtonText: T('SWEETALERT_CONFIRM_BUTTON_TEXT'),
})
.then(result => {
return new Promise(function (resolve, reject) {
resolve(true);
});
})
.catch(result => {
console.log(result);
});
}
我想要实现的是:
sweetAlertConfirmationMessage(T('SWEETALERT_MESSAGE_DELETE_PRODUCT_FROM_CART'))
.then(
// Run code here.
)
但是,我收到以下错误:
Uncaught (in promise) TypeError: Cannot read property 'then' of undefined
这是我观察到的情况:
- sweetAlertConfirmationMessage 方法是运行。
- 在 promise 被 resolve 之前,代码继续离开函数,进入 .then(// 运行 code here)
- .then 不是 运行,因为它没有对 采取行动的承诺
- 显示通知消息,等待用户确认或取消。
console.log(结果)不显示任何内容。
我研究了以下主题:
- 异步并等待
- 承诺、.then 和 .catch
- 承诺链接
如何按所需顺序执行此代码:
- 显示确认信息
- 等待承诺:如果确认则为 true,如果单击其他任何内容则为 undefined
- 运行 .then(//运行 代码)
我希望这个答案写得很好,可以在以后帮助到其他人。
function sweetAlertConfirmationMessage(message) {
return Swal.fire({
title: T('SWEETALERT_GENERAL_CONFIRMATION_TITLE'),
text: message,
icon: 'warning',
showCancelButton: true,
confirmButtonColor: T('SWEETALERT_CONFIRM_BUTTON_COLOR'),
cancelButtonColor: T('SWEETALERT_CANCEL_BUTTON_COLOR'),
cancelButtonText: T('SWEETALERT_CANCEL_BUTTON_TEXT'),
confirmButtonText: T('SWEETALERT_CONFIRM_BUTTON_TEXT'),
})
}
只是 return 您函数的承诺。您可以这样使用:
sweetAlertConfirmationMessage('msg').then(x => {})
阅读有关 Promisechaining 的更多信息: https://javascript.info/promise-chaining