Defoe
Defoe

Reputation: 371

Get result from promise resolve in another async function

I'm trying to get the promise resolve result when it ends the Promise in the first function (topromise). Therefore as you'll see below I'm creating another Promise.resolve(pageData) with the value from the last then to try to get the value in my getpromise function.

This is my code:

function topromise(param){
  let pageData;
  new Promise((resolve, reject)=>{
  resolve(param)
})

.then((value)=>{
  console.log(value)
  return "hola"
})

.then((value)=>{
  console.log(value)
  pageData= "bon jour"
  return getpromise(Promise.resolve(pageData))
})

}


topromise("hello")

function getpromise(value){
  .then(value=> console.log(value))
  //I want to get the pageData result from resolve
}

Upvotes: 0

Views: 521

Answers (1)

JLRishe
JLRishe

Reputation: 101652

Your topromise() function is missing a return, and your getpromise function seems to begin in the middle of a method call.

It's very unclear what you're trying to do, but maybe you're going for something like this. This is working code:

function topromise(param){
  let pageData;
  
  return Promise.resolve(param)
  .then((value)=>{
    console.log(value)
    return "hola"
  })
  .then((value)=>{
    console.log(value)
    pageData = "bon jour"
    return pageData;
  })
}


getpromise(topromise("hello"))

function getpromise(value){
  value
  .then(result => console.log(result))
}

Upvotes: 1

Related Questions