hello all - first time posting here.
i would like somebody else knowledgeable with promises to have a look at my code example & see if this is correct or not.
the code is attempting to simulate three tasks and share parameters. this will be my working example for future projects.
thank you very much for your consideration.
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 ) })
;