Yul*_*ale 5 javascript promise async-await
我正在使用 JavaScript。
我的问题来自这种情况:我有一个正在映射的大数组,
let myNewArray = myLargeArray.map(someFuntion)
console.log(myNewArray)
Run Code Online (Sandbox Code Playgroud)
映射是否可能花费太长时间并且undefined可能被记录?那么我应该使用 async-await 还是只为 Promise 保留?
映射是否可能需要太长时间
"too long" is subjective. The time it takes won't have any impact on the value you end up with though.
that undefined might be logged?
map always returns an array, so no.
The array might contain undefined values though.
So should I use async-await or is it only reserved for promises?
You can only usefully await a promise.
map will return an array, so you can't usefully await it.
If someFunction returns a promise, then the map will return an array of promises, which you could wrap with Promise.all which returns a promise that you could await if you wanted to log an array of resolved values instead of an array of promises.
| 归档时间: |
|
| 查看次数: |
1704 次 |
| 最近记录: |