If you still use this approach, it will be more efficient to initialize the callback functions separately, creating them in the right place.
/** * * (, 1.jpg, 2.jpg, 3.jpg, 4.jpg , * fake.jpg - ) * * @type {string[]} */ var imgList = ["img/1.jpg", "img/2.jpg", "img/fake.jpg", "img/3.jpg", "img/4.jpg"];
/** * * url * * @param url * @returns {Promise} */ function loadImage(url) { // "" return new Promise(function(resolve, reject) { var img = new Image(); img.onload = function() { // , "" url return resolve(url); } img.onerror = function() { // , "" url return reject(url); } img.src = url; }); }
The Promise interface (promise) is a wrapper for a value unknown at the time the promise is created. It allows processing the results of asynchronous operations as if they were synchronous: instead of the final result of the asynchronous method, a promise is returned, the result of which can be obtained at some point in the future.
When creating, the promise is pending (pending state), and then it can become fulfilled, returning the result (value), or rejected, returning the reason for the refusal.
function myPromise() { return new Promise(function(resolve, reject) { // var ascync = true; // false if (!ascync) return reject(new Error(" ...")); return resolve(1); }); } myPromise() .then(function(res) { console.log(res); // 1 }) .catch(function(err){ console.log(err.message); // " ..." });
// var ascync = true; // false if (!ascync) { reject(new Error(" ...")); } else { resolve(1); }
then(function onSuccess(){}, function onFail(){});
myPromise() .then(function(res) { console.log(res); // 1 }) .catch(function(err){ console.log(err.message); // " ..." }); myPromise() .then(function(res) { console.log(res); // 1 }, function(error) { console.log(err.message); // " ..." }); myPromise() .then(function(res) { console.log(res); // 1 }) .then(undefined, function(error) { console.log(err.message); // " ..." });
//, id="images", , div loadImage(imgList[0]) .then(function(url) { $('#images').append('<img src="'+url+'" style="width: 200px;" />'); }) .catch(function(url) { // , , Error //, , , - ... console.log(" : ", url); });
/** * * * @param images - url */ function displayImages(images) { var imgSrc = images.shift(); // if (!imgSrc) return; // // , return loadImage(imgSrc) .then(function(url) { $('#images').append('<img src="'+url+'" style="width: 200px;"/>'); return displayImages(images); // }) .catch(function(url) { // - , console.log(' : ', url); return displayImages(images); // }); }
var promiseImgs = []; promiseImgs = imgList.map(loadImage); // promiseImgs = imgList.map(function(url){ return loadImage(url); });
/** * * @param imgList - url * @returns {Promise} */ function loadAndDisplayImages(imgList) { var notLoaded = [];// url, var loaded = [];// url, var promiseImgs = imgList.map(loadImage); // reduce(...) - Promise, : //loadAndDisplayImages(...).then(...).catch(...); return promiseImgs.reduce(function (previousPromise, currentPromise) { return previousPromise .then(function() { // , previousPromise - resolved (= Promise.resolve()) return currentPromise; }) .then(function(url) // "" resolved { $('#images').append('<img src="'+url+'" style="width: 200px;"/>'); loaded.push(url); return Promise.resolve(url); }) .catch(function(url)// "" rejected { console.log(' : ', url); notLoaded.push(url); return Promise.resolve(url); }); }, Promise.resolve()) .then(function (lastUrl) { console.log('lastUrl:', lastUrl); let res = {loaded: loaded, notLoaded: notLoaded}; // Promise, return Promise.resolve(res); }); } loadAndDisplayImages(imgList) .then(function(loadRes) { console.log(loadRes); }) .catch(function(err) { console.log(err); });
Source: https://habr.com/ru/post/317256/
All Articles