A promise is a
(...) value which may be available now, or in the future, or never (Source: MDN)
So lets say I have an app which wants to work with pictures. The pictures are loaded e.g. after an algorithm works with it in the background (or some other sort of delay). Now I want to check, if the pictures are available in the future, by using a promise, not a callback.
To check, if an image is available, I could use the following code:
function loadImage(url) {
return new Promise((resolve, reject) => {
let image = new Image()
image.onload = function() {
resolve(image)
}
image.onerror = function() {
let message =
'Could not load image at ' + url
reject(new Error(msg))
}
image.src = url
})
}
The promise, which belongs to this would look something like this:
Promise.all([
loadImage('images/pic1.jpg'),
loadImage('images/pic2.jpg')
]).then((images) => {
images.forEach(img => addImg(img.src))
}).catch((error) => {
// handle error
})
This will not help my at all, because the promise will look up if the pictures are available in the present, not in the future. The pictures will be there in a second, still the promise will return an error.
What do I not get about promises here? For me it looks like the entire future aspect of checking, if the pics are available depends on loadImage(url)
not on the actual promise itself.
PS: Example inspired by funfunfunction
How would the promise know when it is resolved?
To my understanding it does not "listen" to the other code around it, let alone a framework which might run in the background. I would assume in this example it would turn out like this: promise checks if pictures are available -> they are still worked with in the background -> promise resolves and throws error -> algorithm is finished with pictures -> pictures are available ?
See: You're Missing the Point of Promises.
Cannot consider all of the nuances relevant to using
Promise
alone. Browse thepromise
tag to read other users' questions and answers posted by users whom post frequently atpromise
tag. Ask questions as to specific cases at comments at those questions and answers, for your own edification.You get the value of a
Promise
using.then()
or.catch()
, not by checking the value of a variable outside ofPromise
chain.Disregard "present" and "future". Focus on implementing code within
.then()
and, or.catch()
. All other code referencing an asynchronous value referenced by an identifier will not be reliable.Note also, function passed to
Promise
constructor as executor is called immediatelyreturn
a value from.then()
.return
a value from.then()
. Use.map()
instead of.forEach()
Use
.map()
instead of.forEach()
A handled rejection or error is converted to a resolved
Promise
is notthrow
n or rejection returned