Skip to content

Instantly share code, notes, and snippets.

@motopig
Created March 18, 2019 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motopig/9e7ee54300db33e2857be858fd58b865 to your computer and use it in GitHub Desktop.
Save motopig/9e7ee54300db33e2857be858fd58b865 to your computer and use it in GitHub Desktop.
use await in foreach
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