如何failed在函式之外獲取陣列的值?因為我需要這個陣列作為其他函式的輸入。
failed = [];
async function fetchFromApi(i) {
var ApiEndpoint = "https://jsonplaceholder.typicode.com/todos/" i
var httpRequest = {
method: "GET"
}
var response = await fetch(ApiEndpoint, httpRequest);
if (!response.ok) { // Necessary for error handling, fetch does not return an error if HTTP request fails
failed.push(i);
console.log(failed)
} else {
let symbolData = await response.json();
console.log(JSON.stringify(symbolData))
return JSON.stringify(symbolData);
}
};
fetchFromApi("abc")
console.log(failed)
所述console.log("failed")代碼中的上述給我一個空陣列[]。我希望它["abc"]在函式外部呼叫此變數時得到
更新編輯(新的后續問題):
我嘗試了 @SlothOverlord 的解決方案,下面的例子似乎有效。
const myFunction = async (j) => {
failed = [];
async function fetchFromApi(i) {
var ApiEndpoint = "https://jsonplaceholder.typicode.com/todos/" i
var httpRequest = {
method: "GET"
}
var response = await fetch(ApiEndpoint, httpRequest);
if (!response.ok) { // Necessary for error handling, fetch does not return an error if HTTP request fails
failed.push(i);
} else {
let symbolData = await response.json();
console.log(JSON.stringify(symbolData))
return JSON.stringify(symbolData);
}
};
await fetchFromApi(j)
return failed
}
myFunction("abc").then(data=>console.log(failed))
但是,當我在forEach函式中添加一條陳述句時,它會中斷并再次回傳一個空陣列。請參閱下面的示例。這里發生了什么?
const myFunction = async (j) => {
failed = [];
async function fetchFromApi(arrays) {
arrays.forEach(async function(i) {
var ApiEndpoint = "https://jsonplaceholder.typicode.com/todos/" i
var httpRequest = {
method: "GET"
}
var response = await fetch(ApiEndpoint, httpRequest);
if (!response.ok) { // Necessary for error handling, fetch does not return an error if HTTP request fails
failed.push(i);
// console.log(failed)
} else {
let symbolData = await response.json();
console.log(JSON.stringify(symbolData))
return JSON.stringify(symbolData);
}
});
}
await fetchFromApi(j)
// console.log(failed)
return failed
}
myFunction(["aaa", "abc"]).then(data=>console.log(failed))
uj5u.com熱心網友回復:
您需要等待 fetchFromApi("abc") 函式。
異步等待僅在函式內部起作用,而不在函式外部起作用。
所以發生的事情是:
fetchFromApi("abc") is called
console.log(failed) is empty
fetchFromApi("abc") finished
解決方案是將函式包裹在 async 中并等待 fetchFromApi 呼叫
const myFunction = async () => {
//...
failed = [];
async function fetchFromApi(i) {
//...
}
await fetchFromApi("abc")
console.log(failed)
}
uj5u.com熱心網友回復:
fetchFromApi 是一種異步方法,如果您想查看記錄的結果,則需要等待它。
JavaScript 有一個基于事件回圈的并發模型,它負責執行代碼、收集和處理事件。當異步呼叫得到它的結果時,它將被放置在回呼佇列中,并且在下一次迭代(滴答)時,它將從那里彈出并放置在執行堆疊中進行處理。當您使用 時await,執行呼叫將在該點停止并繼續進行,直到異步呼叫獲得結果。
failed = [];
async function fetchFromApi(i) {
var ApiEndpoint = "https://jsonplaceholder.typicode.com/todos/" i
var httpRequest = {
method: "GET"
}
var response = await fetch(ApiEndpoint, httpRequest);
if (!response.ok) { // Necessary for error handling, fetch does not return an error if HTTP request fails
failed.push(i);
console.log(failed)
} else {
let symbolData = await response.json();
console.log(JSON.stringify(symbolData))
return JSON.stringify(symbolData);
}
};
await fetchFromApi("abc")
console.log(failed)
JS 事件回圈 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360681.html
標籤:javascript 异步 异步等待 获取-api
上一篇:如何停止異步回圈
