node.js - 在外面使用answer .then()并在node js中的另一部分代码中使用

标签 node.js promise adonis.js

我如何在外部使用 promise 的答案。那我该怎么办?

   arreglo.forEach((item) => {
        if (item.tipoCampo == 3) {
            self.campoSelects(item.tablaCampo)
                .then(resp => {
                    console.log(resp)
                })
                .catch(e => console.log(e))
        }
    });

.then () 内的 console.log (resp) 知道它并正确打印,但是当我想知道 forEach 中的 resp 以便在下面使用时,它显示未定义 谢谢。

最佳答案

arreglo.forEach((item) => {
   if (item.tipoCampo == 3) {
     self.campoSelects(item.tablaCampo)
        .then(resp => {
            logMyData(resp);
        })
         .catch(e => console.log(e))
    }
});

logMyData=(x)=>{
 console.log(x);
}

这就像添加一个在 .then 中执行的辅助函数一样简单

关于node.js - 在外面使用answer .then()并在node js中的另一部分代码中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54947096/

相关文章:

node.js - 用于 Node.js 脚本测试的 mocha 是否具有用于浏览器的 Mocha 的漂亮 html 输出?

javascript - 如何在 Node.js 中解析数组?

javascript - 正确使用 Promises 和 Await/Async

javascript - 如何从使用 Adonis Controller 的路由在 Vue 组件中发出 axios 请求

node.js、Rabbitmq 和 Docker : the service using seneca seems to start before rabbitmq

node.js - Node.js 中异步/等待的行为

react-native - 使用 map 功能异步等待,promise.all 不起作用

node.js - HasMany 关系在使用 mongodb 数据库的 Adonis 框架中不起作用

node.js - 使用 adonis migration/nodejs 的表中的多个主键

javascript - 通过 JavaScript 对象的引用传递/比较