Javascript 等待异步调用后未填充在函数外部定义的数组

Javascript 等待异步调用后未填充在函数外部定义的数组,javascript,async-await,Javascript,Async Await,我有以下代码,其中在getStuff函数中,我正在进行一个外部异步调用,并将结果存储在result中。从那里,我循环遍历result.Content.Ids并将Ids推入myArray 当在run()中调用getStuff()时,我需要进一步访问myStuff数组的代码,但如果我将其从console.log中注销,它将显示为空 const myArray = []; const getStuff = async (val) => { const other = {} i

我有以下代码,其中在
getStuff
函数中,我正在进行一个外部异步调用,并将结果存储在
result
中。从那里,我循环遍历
result.Content.Ids
并将Ids推入
myArray

当在
run()
中调用
getStuff()
时,我需要进一步访问
myStuff
数组的代码,但如果我将其从console.log中注销,它将显示为空

const myArray = [];

const getStuff = async (val) => {
    const other = {}

    if(val) other.newestVal = val;

    const result = await someCall(other);

    result.Content.Ids.forEach((id) => myArray.push(id));

    if (result.AFieldExists) {
        getStuff(result.newVal)
    }
}

const run = async () => {
    await getStuff();

    // other code below that uses myArray
    // but its empty when I log it
}

run();
由于我正在等待
run()
中的
getStuff()
,我本来希望填充
myArray
。我做错了什么?

您的递归调用:

if (result.AFieldExists) {
    getStuff(result.newVal)
}
不正确,因为您没有等待结果-将只等待第一个
getStuff
调用。你需要:

const getStuff = async (val) => {
    const other = {}

    if(val) other.newestVal = val;

    const result = await someCall(other);

    result.Content.Ids.forEach((id) => myArray.push(id));

    if (result.AFieldExists) {
        return getStuff(result.newVal)
    }
}
您还可以稍微清理一下,以避免出现丑陋的外部变量:

const getStuff = async (val, output = []) => {
    const other = {}
    if (val) other.newestVal = val;
    const result = await someCall(other);
    output.push(...result.Content.Ids);
    return result.AFieldExists
        ? getStuff(result.newVal, output)
        : output;
}

const run = async () => {
    const output = await getStuff(); // pass initial value here?
}
您的递归调用:

if (result.AFieldExists) {
    getStuff(result.newVal)
}
不正确,因为您没有等待结果-将只等待第一个
getStuff
调用。你需要:

const getStuff = async (val) => {
    const other = {}

    if(val) other.newestVal = val;

    const result = await someCall(other);

    result.Content.Ids.forEach((id) => myArray.push(id));

    if (result.AFieldExists) {
        return getStuff(result.newVal)
    }
}
您还可以稍微清理一下,以避免出现丑陋的外部变量:

const getStuff = async (val, output = []) => {
    const other = {}
    if (val) other.newestVal = val;
    const result = await someCall(other);
    output.push(...result.Content.Ids);
    return result.AFieldExists
        ? getStuff(result.newVal, output)
        : output;
}

const run = async () => {
    const output = await getStuff(); // pass initial value here?
}

您需要将您的
wait
调用放入
try{}catch(){}
, 您对
getStuff()
的递归调用也没有包含关键字
wait

    async function someCall() {
  return {
    Content: {Ids: [1, 2, 3]}
  }
}


const myArray = [];



const getStuff = async (val) => {
    const other = {}
    let result;
    if(val) other.newestVal = val;
try{
    result = await someCall(other);
}catch(err){
console.log('error from some call' ,err);
}
console.log(myArray)
    result.Content.Ids.forEach((id) => myArray.push(id));
console.log(myArray)

    if (result.AFieldExists) {
        try{
        await getStuff(result.newVal)
        }catch(err){
            console.log('error from get stuff in get stuff' , err);
        }
    }
}

const run = async () => {
    console.log("run")
  try{
    await getStuff();
  }catch(err){
      console.log('error' , err);
  }
    console.log("end run")
console.log(myArray)
    // other code below that uses myArray
    // but its empty when I log it
}

run();

您需要将您的
wait
调用放入
try{}catch(){}
, 您对
getStuff()
的递归调用也没有包含关键字
wait

    async function someCall() {
  return {
    Content: {Ids: [1, 2, 3]}
  }
}


const myArray = [];



const getStuff = async (val) => {
    const other = {}
    let result;
    if(val) other.newestVal = val;
try{
    result = await someCall(other);
}catch(err){
console.log('error from some call' ,err);
}
console.log(myArray)
    result.Content.Ids.forEach((id) => myArray.push(id));
console.log(myArray)

    if (result.AFieldExists) {
        try{
        await getStuff(result.newVal)
        }catch(err){
            console.log('error from get stuff in get stuff' , err);
        }
    }
}

const run = async () => {
    console.log("run")
  try{
    await getStuff();
  }catch(err){
      console.log('error' , err);
  }
    console.log("end run")
console.log(myArray)
    // other code below that uses myArray
    // but its empty when I log it
}

run();

尝试在递归调用中等待
getStuff()
too@VLAZ这可能是因为您的代码示例没有进行递归调用。@Yousaf它应该是一个温和的提醒,提醒您在文章中没有递归调用。但你是对的。明确地说,“这个代码不起作用”,但没有说明它应该如何工作,以及当它失败时,我们需要做更多的工作来回答,不应该这样做。鉴于模仿
someCall(参见代码片段)是多么容易,我们没有理由拒绝复制步骤
在递归调用中too@VLAZ这可能是因为您的代码示例没有进行递归调用。@Yousaf它应该是一个温和的提醒,提醒您在文章中没有递归调用。但你是对的。明确地说,“这个代码不起作用”,但没有说明它应该如何工作,以及当它失败时,我们需要做更多的工作来回答,不应该这样做。考虑到模仿
someCall
(参见代码片段)是多么容易,我们没有理由拒绝复制步骤。不过,这里我不想返回
结果。我想返回
输出
数组。我也需要等待getStuff递归调用吗?这里我不想返回
result
。我想返回
输出
数组。我还需要等待getStuff递归调用吗?