【发布时间】:2022-01-03 12:28:47
【问题描述】:
我有这个函数 getData 将从 JSON 文件中读取 ID 列表。 ID 列表存储到我需要在另一个函数中使用的 idsList 变量中。我如何从这个函数返回值
function getData(){
fetch("JSONitemIDsList.json")
.then(response => response.json())
.then(data => {
let idsList = data.ids
//return idsList
})
}
function myFunc(idsList) {
//do something with idsList
}
【问题讨论】:
-
你可以使用异步等待
-
如果你在成功的
then调用中调用myFunc怎么办?.then(data => { let idList = data.ids; myFunc(idList); })
标签: javascript function return arrow-functions