edwardsmarkf
Coder
hello all, first time on this forum.
i have serious problems trying to wrap my head around promises, so i have decided to just keep a couple of working examples around so i can refer back to those. here is an example i was able to put together:
this is just intended to demonstrate three different promises chained together, just doing a "setTimeout" as an example. I would appreciate any opinions on my example, anything i am missing? etc.
thank you all very much.
i have serious problems trying to wrap my head around promises, so i have decided to just keep a couple of working examples around so i can refer back to those. here is an example i was able to put together:
JavaScript:
const firstPromise = (promiseInput) => {
return new Promise( (resolve, reject) => {
console.log(promiseInput);
let returnStuff = promiseInput + ' - parameter passed into first promise. ' ;
setTimeout( () => {
console.log ('waiting a half-second, resolving: ' + returnStuff);
resolve (returnStuff);
},500 );
// we could use the "reject" function if this promise failed!
})
}
const secondPromise = (promiseInput) => {
return new Promise( (resolve, reject) => {
console.log(promiseInput);
let returnStuff = promiseInput + ' - parameter passed into second promise. ' ;
setTimeout( () => {
console.log ('waiting a half-second, resolving: ' + returnStuff);
resolve (returnStuff);
},500 );
})
}
const thirdPromise = (promiseInput) => {
return new Promise( (resolve, reject) => {
console.log(promiseInput);
let returnStuff = promiseInput + ' - parameter passed into third promise. ' ;
setTimeout( () => {
console.log ('waiting a half-second, resolving: ' + returnStuff);
resolve (returnStuff);
},500 );
})
}
firstPromise('one')
.then( value => { return secondPromise (value + ' two') })
.then( value => { return thirdPromise (value + ' three') })
.then( value => { console.log(' FINAL result: ' + value) })
.catch( error => { console.log(' ERROR! ' + error ) })
;
this is just intended to demonstrate three different promises chained together, just doing a "setTimeout" as an example. I would appreciate any opinions on my example, anything i am missing? etc.
thank you all very much.