我正在尝试在所有先前的函数完成后执行我的函数 HideEmptyTile。
我一直在使用 TimeOut 直到现在,但这并不是完成我想要完成的事情的正确方法。
这是我之前的代码:
retrieveAccountOpenWithVisitObjectif(userId, processOpenWithVisitObjectif);
retrieveAccountOpen(userId, processAccountOpen);
retrieveAllActivitiesOpen(userId, processAllActivitiesOpen);
setTimeout("HideEmptyTile()", 800);
经过一些研究,我发现我可以使用其他回调函数,但 Promise.all 似乎是为我的案例构建的。所以我做了下一个代码:
Promise.all(
retrieveAccountOpenWithVisitObjectif(userId, processOpenWithVisitObjectif),
retrieveAccountOpen(userId, processAccountOpen),
retrieveAllActivitiesOpen(userId, processAllActivitiesOpen)).then(HideEmptyTile());
问题是这不是我想要的行为,因为 HideEmptyTile 函数不会在其他函数之后执行。
我是否以错误的方式使用了 Promise.all?
最佳答案
Promise.all
采用一组 promise ,而不是多个参数then
接受一个函数(您已经调用它并传递了结果)
Promise.all([
retrieveAccountOpenWithVisitObjectif(userId, processOpenWithVisitObjectif),
retrieveAccountOpen(userId, processAccountOpen),
retrieveAllActivitiesOpen(userId, processAllActivitiesOpen)
]).then(HideEmptyTile);
关于javascript - 使用 Promise.all 在其他函数之后执行一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46037207/