Coding is as much a matter of personal growth as it is of logic and control-flow. I keep patience, curiosity, & exuberance in the same toolbox as vim and git.
*Opinions posted are my own*
In fact, we can say that JavaScript arrays are envelopes that have the special property of including multiple values.
Promises are similar. They are envelopes just like arrays are, but the special property they have is they handle future/potential/asynchronous values.
constf=x=>newPromise((res,rej)=>x?res(x):rej(x))f(2).then(x=>x*2).then(console.log)// 4f(null).then(x=>x*2)// this function never runs.catch(x=>console.error(`promise rejected for ${x}`))// promise rejected for null
This is especially useful for managing asynchronous problems. By using a functional approach, Promise flows can be written in a top-down, left-to-right manner:
consthandleAsText=response=>response.text()constunsafeDocumentWrite=text=>{document.open();document.write(text);document.close();}fetch('google.com')// fetch API returns a Promise<Response>.then(handleAsText).then(unsafeDocumentWrite)
In fact, we can say that JavaScript arrays are envelopes that have the special property of including multiple values.
Promises are similar. They are envelopes just like arrays are, but the special property they have is they handle future/potential/asynchronous values.
Coding is as much a matter of personal growth as it is of logic and control-flow. I keep patience, curiosity, & exuberance in the same toolbox as vim and git.
*Opinions posted are my own*
Think of a regular JS array as an envelope. You put one (or more) values into the envelope, and it gives you extra behaviour like
map
andreduce
In fact, we can say that JavaScript arrays are envelopes that have the special property of including multiple values.
Promises are similar. They are envelopes just like arrays are, but the special property they have is they handle future/potential/asynchronous values.
This is especially useful for managing asynchronous problems. By using a functional approach, Promise flows can be written in a top-down, left-to-right manner:
I get it now...
Thanks a lot!
Yup!
The non-eli5 answer is that Promises are (not 100% but basically) monads that encode asynchronous behaviour
Got it. :)