Created
January 17, 2018 08:49
-
-
Save dmitryshelomanov/d23b596e6e95260e3b0b7008a1711666 to your computer and use it in GitHub Desktop.
This file contains 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
// обьект маппера. Ф-я mapper будет для каждого промиса вызываться | |
// и колбэки что бы отследить ошибки и конец маппера | |
const mapObj = { | |
arr: [], | |
errorCb(error) { | |
console.log('error') | |
}, | |
resolveCb(lastResolve) { | |
console.log('resolveCb', lastResolve) | |
}, | |
mapper(mapData, nextHtml) { | |
return new Promise(async (res, rej) => { | |
try { | |
res(await mapData(nextHtml)) | |
} | |
catch (error) { | |
rej() | |
} | |
}) | |
}, | |
} | |
// сам маппер | |
function asyncMap({ arr, errorCb, resolveCb, mapper }) { | |
let q = Promise.resolve() | |
Promise.all(arr.map((v) => { | |
return q = q.then((data = null) => { | |
return mapper(v, data) | |
}) | |
})) | |
.then(end => { | |
resolveCb(end[end.length - 1]) | |
}) | |
.catch(error => { | |
errorCb(error) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment