Skip to content

Instantly share code, notes, and snippets.

@LinZap
Created June 9, 2019 14:22
Show Gist options
  • Select an option

  • Save LinZap/ea96464580402e1a3b102cb2a80b3d8a to your computer and use it in GitHub Desktop.

Select an option

Save LinZap/ea96464580402e1a3b102cb2a80b3d8a to your computer and use it in GitHub Desktop.
Node.js - HK7
async function map(arr, func) {
for (let i = 0; i < arr.length; i++)
arr[i] = await func(arr[i])
return arr
}
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
; (async () => {
let arr = [1, 2, 3, 4]
arr = await map(arr, async (ele) => {
await delay(1000) // 等待 1 秒
return ele * 100 // 回傳 *100 結果
})
console.log(arr); // 要等 4 秒才會印出
})()
[ 100, 200, 300, 400 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment