Created
January 17, 2018 10:53
-
-
Save dmitryshelomanov/f77a53bdab3f1b80d34048706549d801 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
// маппер. Запускает цепочку | |
module.exports = function asyncMap(arr, mapper, initialData) { | |
let q = Promise.resolve() | |
return Promise | |
.all(arr.map(v => q = q.then((data = initialData) => mapper(v, data)))) | |
.then(resolve => resolve[resolve.length - 1]) | |
} | |
// так выглядит юзание | |
asyncMap(rs, mapperFn, $.html()) | |
.then((lastResolve) => { | |
console.log('lastResolve', lastResolve) | |
}) | |
.catch((error) => { | |
console.log('error', error) | |
}) | |
// вот тут маппер. И я проебал error в rej. Вот ошибка не приходила | |
function mapperFn(mapData, nextHtml) { | |
return new Promise(async (res, rej) => { | |
try { | |
res(await mapData(nextHtml)) | |
} | |
catch (error) { | |
rej(error) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mapData
это функция итерируемая