We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
每日一题会在下午四点在交流群集中讨论,五点 Github、交流群同步更新答案
The text was updated successfully, but these errors were encountered:
const arr = [1, 2, 3, 4, 5]; function getData() { return new Promise((resolve, reject) => { setTimeout(() => { resolve("data"); }, 1000); }); } (async () => { const result = arr.map(async () => { console.log("start"); const data = await getData(); console.log(data); return data; }); console.log(result); })(); // 5 start -> 遍历每一项开始 // (5) [Promise, Promise, Promise, Promise, Promise] -> 返回的结果 // 5 data -> 遍历每一项异步执行返回的结果
map 函数的原理是:
map 函数函数是同步执行的,循环每一项时,到给新数组值都是同步操作。
代码执行结果:
map 不会等到回调函数的异步函数返回结果,就会进入下一次循环。
执行完同步操作之后,就会返回结果,所以 map 返回的值都是 Promise
简单实现一个
// 获取数据接口 function getData() { return new Promise((resolve, reject) => { setTimeout(() => { resolve("data"); }, 1000); }); } // 异步的map async function selfMap(arr, fn) { let result = []; for (let i = 0, len = arr.length; i < len; i++) { const item = await fn(arr[i], i); result.push(item); } return result; } // 调用 (async () => { const res = await selfMap([1, 2, 3, 4, 5], async (item, i) => { const data = await getData(); return `${item}_${data}`; }); console.log(res, "res"); })(); // ["1_data", "2_data", "3_data", "4_data", "5_data"] "res"
function getData() { return new Promise((resolve, reject) => { setTimeout(() => { resolve("data"); }, 1000); }); } (async () => { for (let i = 0, len = arr.length; i < len; i++) { console.log(i); const data = await getData(); console.log(data); } })(); // 0 // data // 1 // data // 2 // data // 3 // data // 4 // data
Sorry, something went wrong.
No branches or pull requests
扫描下方二维码,收藏关注,及时获取答案以及详细解析,同时可解锁800+道前端面试题。
The text was updated successfully, but these errors were encountered: