Promise.all
returns a single Promise that fulfills when all of the promises passed as an iterable have been fulfilled. Having this in mind, how is it possible to check if only one of the promises is fulfilled? I have a piece of code shown below:
Promise.all([getPic('blah.jpg'), getPic('blah_two.jpg')]).then(
function(result) {
$('#div').hide();
}
).catch(function(result) {
$('#div').show();
})
Here, I want to wait for any of the images, that is blah.jpg and blah_two.jpg, to be loaded. If any of them is loaded then $('#div').hide();
. However, my code checks for both of the images to be loaded, which is not what I actually have in mind. Could you please help me with the problem, and hide the $('#div')
the moment any of the images is loaded.
There is one Promise.any
which I think would do what I have in mind; however, as Mozilla says "is experimental and not fully supported by all browsers."
By the way, the following is my getPic function:
function getPic(src, div) {
return new Promise(function(resolve, reject) {
$(div).attr('src', src).on("error", function(e) {
reject( "Cannot access " + src );
})
.on("load", function(e) {
resolve();
});
})
}