Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.
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:
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.
 
i received some excellent feedback from sitepoint:


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!
    })
}
                               // shorter version per "windbeneathmywings"
const secondPromise = (promiseInput) => 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) => 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 =>          thirdPromise  (value + ' three')   )  // shorter version per "windbeneathmywings"
  .then(  value =>          console.log(' FINAL result: ' + value)   )
  .catch( error => { console.log(' ERROR! ' + error )         })
  ;
 
Last edited by a moderator:
Back
Top Bottom