Created
June 9, 2019 14:22
-
-
Save LinZap/ea96464580402e1a3b102cb2a80b3d8a to your computer and use it in GitHub Desktop.
Node.js - HK7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 秒才會印出 | |
| })() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ 100, 200, 300, 400 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment