等待两个异步函数

时间:2018-05-31 19:00:30

标签: javascript asynchronous google-visualization

我遇到的问题是我想根据fetch返回的数据绘制图表。我知道我可以做以下

google.charts.setOnLoadCallback(drawChart);

function drawChart() {
    // get the data and draw the chart after
    fetch(url).then...
}

但是,我想做这样的事情

// load google charts
google.charts.setOnLoadCallback(???);
// get the data
fetch(url).???;

// a function that runs after both are complete
drawsChartsAfterBothComplete();

这样,数据和谷歌库可以异步加载,而不必等待另一个。这可能吗?如果是这样,我怎么能在我的场景中做到这一点?感谢

修改

我已经确定无需使用setOnLoadCallback(),而load()会因为接受的答案而返回承诺。

1 个答案:

答案 0 :(得分:4)

Promise.all()可能就是你要找的东西。

您可以在promises中运行这两个,并在完成后运行draw函数。

public_html