简体   繁体   English

function 没有在 Promise all 的 then 部分中调用

[英]function not being called in the then section of Promise all

Inside my componentdidmount function of a react native component is this code在我的componentdidmount function 的 react native 组件里面是这个代码

await Promise.all([
        this.fetchDataFromExchange(timeframes[1],false),
        this.fetchDataFromExchange(timeframes[2],false),
        this.fetchDataFromExchange(timeframes[3],false),
        this.fetchDataFromExchange(timeframes[4],false)
    ]).then(() => {this.createConfluenceSummaryTable()})

If the last line of code reads like this如果最后一行代码是这样的

]).then(console.log("ALL DONE"))

Then i see then message in the console after everything is done, which is correct behaviour.然后我在一切完成后在控制台中看到消息,这是正确的行为。 So i know the promise all section works.所以我知道 promise 所有部分都有效。 What doesnt work though is when i try and call the createConfluenceSummaryTable function.但是当我尝试调用createConfluenceSummaryTable function 时不起作用。 This function is part of the same component so I would usually call it using the this.这个 function 是同一个组件的一部分,所以我通常会使用this. Inside that function is another console.log statement saying im running but it never displays which leads me to believe that it is never called.在 function 里面是另一个 console.log 语句,说im running ,但它从不显示,这让我相信它永远不会被调用。

Why is the function not called?为什么没有调用 function? How do I call it properly?我该如何正确称呼它?

Ive tried我试过了

]).then(result => {createConfluenceSummaryTable})

]).then(result => {this.createConfluenceSummaryTable()})

]).then(this.createConfluenceSummaryTable())

]).then(createConfluenceSummaryTable())

but nothing seems to trigger it但似乎没有什么能触发它

you shouldn't need the .then call since you are using async/await:您不需要.then调用,因为您使用的是 async/await:

try {
    await Promise.all([
        this.fetchDataFromExchange(timeframes[1],false),
        this.fetchDataFromExchange(timeframes[2],false),
        this.fetchDataFromExchange(timeframes[3],false),
        this.fetchDataFromExchange(timeframes[4],false)
    ]);
    this.createConfluenceSummaryTable()
} catch (e) {
    // something failed, check `e`
}

Your .then(console.log()) is also not correct - you need to pass a function in .then and not call it directly there: () => console.log(...) .您的.then(console.log())也不正确 - 您需要在 .then 中传递一个.then而不是直接在那里调用它: () => console.log(...)

Most likely one of your promises has an error which you can catch with a try/catch.您的一个承诺很可能有一个错误,您可以使用 try/catch 捕获该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM