使用 Object-literals 和 switch-case 时无法访问异步函数中的对象键,我做错了什么?

Can't access object key in async function when using Object-literals and switch-case, What am i doing wrong?

提问人:penk naja 提问时间:2/15/2023 最后编辑:penk naja 更新时间:2/16/2023 访问量:81

问:

在这里,我试图从函数中获取一个值,在这种情况下,我的函数返回一个对象,当我尝试使用键访问它时,我得到“未定义”为 bar1,但 bar2 它有一个对象的值。

const foo = async()=> {
    let zoo
    let data = [
        {
            key1:"1",
            key2:"a",
            key3:3,
            key4:"cal",
        }
    ]
    
    for (const iterator of data) {
        // console.log(iterator)
            bar1 = {
                objectLiteral_swithAsync:  await objectLiteral_swithAsync(iterator["key4"]).exe1,
                objectLiterals_withOutAsync :   objectLiterals_withOutAsync(iterator["key4"]).exe1,
                switch_WithAsync : await switch_WithAsync(iterator["key4"]).exe1,
                switch_WithOutAsync :  switch_WithOutAsync(iterator["key4"]).exe1,
            }
            bar2 ={
                objectLiteral_swithAsync:  await objectLiteral_swithAsync(iterator["key4"]),
                objectLiterals_withOutAsync :   objectLiterals_withOutAsync(iterator["key4"]),
                switch_WithAsync : await switch_WithAsync(iterator["key4"]),
                switch_WithOutAsync :  switch_WithOutAsync(iterator["key4"]),
            }
            zoo = {

                bar1 :bar1,
                bar2: bar2
            }
    }
    return zoo
}

async function objectLiteral_swithAsync(param) {

    let obj=  {
       
        'cal': 2 * 2
    }[param]
    let result = {
        exe1 : obj,
        exe2: 2
    }
    return result
}

 function objectLiterals_withOutAsync(param) {
    
    let obj=  {
      
       'cal': 2 * 2
    }[param]
    let result = {
        exe1 : obj,
        exe2: 2
    }
    
    return result
}
async function switch_WithAsync (param){
    let obj
    switch (param) {
        case "cal":
            obj = 2 * 2
            break;
    
        default:
            obj =0
            break;
            
    }
     result = {
        exe1 : obj,
        exe2: 2
    }
    return result
}

function switch_WithOutAsync  (param){
    let obj
    switch (param) {
        case "cal":
            obj = 2 * 2
            break;
    
        default:
            obj =0
            break;
    }
     result = {
        exe1 : obj,
        exe2: 2
    }
    return result
}

foo().then( result=>{
    console.log('--->1',result)
})
// console.log('2', foo())

当对象 bar1 使用表示法调用 asyncfunction 时,结果未定义,但在 bar2 中,每个键都有一个值。.

JavaScript async-await switch-statement 对象 literal

评论


答:

0赞 trincot 2/16/2023 #1

Promise 没有属性,但您的代码正在尝试从 promise 对象中检索此类属性。.exe1

在此表达式中:

 await objectLiteral_swithAsync(iterator["key4"]).exe1

...评估顺序如下:

  1. iterator["key4"]计算结果为“Cal”
  2. objectLiteral_swithAsync(iterator["key4"])计算结果为 Promise(函数返回 Promise)async
  3. <a promise>.exe1在访问不存在的属性时计算为undefined
  4. await undefined将异步挂起要恢复的功能,然后计算结果为undefined

你真正想要的,是在你已经等待承诺并拥有它实现的价值时能够实现。因此,用括号更改执行顺序:.exe1

 (await objectLiteral_swithAsync(iterator["key4"])).exe1

现在评估的顺序如下:

  1. iterator["key4"]计算结果为“Cal”
  2. objectLiteral_swithAsync(iterator["key4"])评估为承诺。
  3. await <promise>挂起函数,直到 promise 得到解决。当它异步恢复时,此表达式的计算结果为{exe1: 4, exe2: 2}
  4. ({exe1: 4, exe2: 2}).exe1评估结果为 4

const foo = async()=> {
    let zoo;
    let data = [{
        key1:"1",
        key2:"a",
        key3:3,
        key4:"cal",
    }];
    
    for (const iterator of data) {
        const bar1 = {
            objectLiteral_swithAsync: (await objectLiteral_swithAsync(iterator["key4"])).exe1,
            objectLiterals_withOutAsync: objectLiterals_withOutAsync(iterator["key4"]).exe1,
            switch_WithAsync: (await switch_WithAsync(iterator["key4"])).exe1,
            switch_WithOutAsync: switch_WithOutAsync(iterator["key4"]).exe1,
        };
        const bar2 = {
            objectLiteral_swithAsync: await objectLiteral_swithAsync(iterator["key4"]),
            objectLiterals_withOutAsync: objectLiterals_withOutAsync(iterator["key4"]),
            switch_WithAsync: await switch_WithAsync(iterator["key4"]),
            switch_WithOutAsync: switch_WithOutAsync(iterator["key4"]),
        };
        zoo = {
            bar1,
            bar2
        };
    }
    return zoo;
}

async function objectLiteral_swithAsync(param) {
    const obj = {       
        cal: 2 * 2
    }[param];
    const result = {
        exe1: obj,
        exe2: 2
    }
    return result;
}

function objectLiterals_withOutAsync(param) {
    const obj = {      
       cal: 2 * 2
    }[param];
    const result = {
        exe1 : obj,
        exe2: 2
    };
    return result;
}

async function switch_WithAsync (param){
    let obj;
    switch (param) {
        case "cal":
            obj = 2 * 2;
            break;
        default:
            obj = 0;
            break;
    }
    const result = {
        exe1: obj,
        exe2: 2
    }
    return result;
}

function switch_WithOutAsync  (param){
    let obj;
    switch (param) {
        case "cal":
            obj = 2 * 2;
            break;
        default:
            obj = 0;
            break;
    }
    const result = {
        exe1: obj,
        exe2: 2
    };
    return result;
}

foo().then(result => {
    console.log('--->1', result)
});

评论

1赞 penk naja 2/16/2023
谢谢你,先生,这就是我一直在寻找的答案。