我如何从api使用我的数据并将其传递到另一个函数进行进一步处理



有什么方法可以不使用setTimeout()来允许从读取的数据存储在stored_data数组中,然后允许测试函数进行操作呢?

到目前为止我还没有定义。

let stored_data = []

fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => stored_data.push(json))

function test(e) {
console.log(e)
}

您可以简单地链接另一个.then。您还忘记将store_data声明为变量

let stored_data = []
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => stored_data.push(json))
.then( _ => {
// array now ready for manipulation
})

最新更新