概念:异步 - 使用 Promise{<Pending>} 等待返回结果

时间:2021-06-19 06:02:03

标签: javascript async-await es6-promise

我遇到了 Async 和 await 的问题之一。它返回 Promise 数组而不是实际的值数组。我了解 async await 在内部使用 Promise 和生成器,但想了解如何解决以下问题。

let object = [
    "hello1",
    "hello2",
    "hello3",
    "hello4"
]

async function funct(value){
    return new Promise((res,rej)=> res( value + '-world'));
}



let resultArray = object.map(async (e)=>{
    let result = await funct(e);
    return result
})


console.log(Promise.all(resultArray).then((r)=> console.log(r)));

输出是:

Promise { <pending> }
[ 'hello1-world', 'hello2-world', 'hello3-world', 'hello4-world' ]

但我正在寻找实际的值数组。

[ 'hello1-world', 'hello2-world', 'hello3-world', 'hello4-world' ]

你能就此提出建议吗?

0 个答案:

没有答案