首页 > 解决方案 > 是否可以在 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()someStufffunctionToRunAfterWardssomeFunctioncode 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());
  },
};

关于如何做到这一点的任何想法?

标签: javascriptnode.jsarraysobjectpromise

解决方案


Promises 的then接受函数。所以,它应该是:

blahBlahPromise.then(functionToRunAfterWards)

推荐阅读