javascript - 是否可以在 then() 之后运行一个方法,该方法在 JavaScript 中返回一个对象?
问题描述
我试图在.then()
方法运行后立即运行一个动作并返回一个object
. 为简单起见,假设我有这个代码片段:
const functionToRunAfterWards = () => { // does stuff };
module.exports = {
someFunction: ({ someParam }) => {
return anotherFunction.then((someValue) => {
// do a bunch of stuff
return someStuff
})
},
};
上面的代码片段有效,但我想在返回function
后运行。我尝试在外部运行该功能,但它说。我也尝试添加另一个,但它不会工作......像这样:then()
someStuff
functionToRunAfterWards
someFunction
code cannot be accessed
.then()
const functionToRunAfterWards = () => { // does stuff };
module.exports = {
someFunction: ({ someParam }) => {
return anotherFunction.then((someValue) => {
// do a bunch of stuff
return someStuff
}).then(functionToRunAfterWards());
},
};
关于如何做到这一点的任何想法?
解决方案
Promises 的then
接受函数。所以,它应该是:
blahBlahPromise.then(functionToRunAfterWards)
推荐阅读
- arrays - 在单元格或单元格区域中将函数作为数组添加
- android - 谷歌地图集成后我的应用程序无法打开
- callback - Javascript中Callback的严格定义
- terraform - 如何在 terraform 中运行 powershell 脚本?
- amazon-web-services - 使用身份池(联合身份)时如何通过管理员创建用户
- c++ - 简单的阶乘查找函数
- excel - VBA 中的 Excel Solver 给出的结果与手动执行 Solver 不同
- node.js - 在 Docker 中同时支持 .net core 2.1+ 和(节点或 PowerShell?)的 .NET Core 映像
- ruby-on-rails - Rails 应用程序中未弹出模态窗口
- grep - 在 alpine 与 ubuntu 上使用来自 nc 的管道的 grep 的不同行为