Last active
July 9, 2024 18:59
-
-
Save HDDen/1e4b609bd565f407e2bb15117ee5fcf8 to your computer and use it in GitHub Desktop.
Promise cycle example
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
var temporaryPromises = block.getAttribute('data-imgs').split(',').map(function(src){ | |
return new Promise(function (resolve, reject) { | |
return resolve(true); | |
// return resolve(false); // надо явно передавать результат + Promise.all прервется если будет хотя бы один reject | |
}); | |
}); | |
Promise.all(temporaryPromises).then(function (arr) { // в arr будет массив результатов - в данном случае, true/false | |
console.log(arr); | |
}); | |
/*************************************************************************************/ | |
const promiseArray = channelNames.map(function (name) { | |
return new Promise(function (resolve, reject) { | |
request(`https://www.googleapis.com/youtube/v3/channels?key=${key}&forUsername=${name}&part=id`, function (error, response, body) { | |
if (error) { | |
callback(error); return reject(error); | |
} else { | |
var data = JSON.parse(body); | |
if (data.items.length == 0) { | |
callback(`Error: No channel id found for ${name}`); | |
return reject(`Error: No channel id found for ${name}`); | |
} else { | |
return resolve(data.items[0].id); | |
} | |
} | |
}); | |
}); | |
}); | |
Promise.all(promiseArray).then(function (channelIds) { | |
console.log(channelIds); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment