Created
March 18, 2019 07:20
-
-
Save motopig/9e7ee54300db33e2857be858fd58b865 to your computer and use it in GitHub Desktop.
use await in foreach
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 Process(source, signature) { | |
const unspents = [1]; | |
return new Promise((resolve, reject) => { | |
const len = unspents.length; | |
asyncForEach(unspents, async (k) => { | |
const encode = await sleepFunc(source) | |
if (!encode) { | |
reject() | |
} | |
if (k === (len-1)) { | |
// todo | |
resolve(signature+'xx'); | |
} | |
}) | |
}); | |
} | |
async function sleepFunc(source) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(source); | |
},3000); | |
}) | |
} | |
async function asyncForEach(array, callback) { | |
for (let index = 0; index < array.length; index++) { | |
await callback(index) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment