react native get response from then and assign to variable












0















I am trying to get thumbnail path and storing to a variable to be used, But I am getting undefined



getThumbnail(filePath){
let thumbnailURL = RNThumbnail.get(filePath)
.then((response) => response.path)
.then((responseData) => {
console.warn(responseData);
return responseData;
}).catch(error => console.warn(error));
alert(thumbnailURL);
//return thumbnailURL;
}









share|improve this question





























    0















    I am trying to get thumbnail path and storing to a variable to be used, But I am getting undefined



    getThumbnail(filePath){
    let thumbnailURL = RNThumbnail.get(filePath)
    .then((response) => response.path)
    .then((responseData) => {
    console.warn(responseData);
    return responseData;
    }).catch(error => console.warn(error));
    alert(thumbnailURL);
    //return thumbnailURL;
    }









    share|improve this question



























      0












      0








      0








      I am trying to get thumbnail path and storing to a variable to be used, But I am getting undefined



      getThumbnail(filePath){
      let thumbnailURL = RNThumbnail.get(filePath)
      .then((response) => response.path)
      .then((responseData) => {
      console.warn(responseData);
      return responseData;
      }).catch(error => console.warn(error));
      alert(thumbnailURL);
      //return thumbnailURL;
      }









      share|improve this question
















      I am trying to get thumbnail path and storing to a variable to be used, But I am getting undefined



      getThumbnail(filePath){
      let thumbnailURL = RNThumbnail.get(filePath)
      .then((response) => response.path)
      .then((responseData) => {
      console.warn(responseData);
      return responseData;
      }).catch(error => console.warn(error));
      alert(thumbnailURL);
      //return thumbnailURL;
      }






      reactjs react-native






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 16:05









      Robbie Milejczak

      2,8861727




      2,8861727










      asked Jan 2 at 15:42









      Jothi KannanJothi Kannan

      2,10542147




      2,10542147
























          2 Answers
          2






          active

          oldest

          votes


















          3














          .then doesn't work like that, it won't return a value. You could do:



            let thumbnailURL;

          RNThumbnail.get(filePath)
          .then((response) => response.path)
          .then((responseData) => {
          thumbnailURL = responseData;
          alert(thumbnailURL);
          }).catch(error => console.warn(error));


          but you have to continue computation inside the second then call because the value is only going to be reliable there



          You're better off using async/await, just refactor your code to this:



          async function getThumbnail(filePath){
          try {
          let thumbnailURL = await RNThumbnail.get(filePath)
          alert(thumbnailURL)
          } catch(err) {
          console.warn(err)
          }


          read more about async / await






          share|improve this answer


























          • tried 1st answer getting same undefined error, 2nd answer throwing error

            – Jothi Kannan
            Jan 2 at 16:11











          • well it's a little weird for you to resolve it twice, what is the purpose of your second .then call? I changed the second example, try that

            – Robbie Milejczak
            Jan 2 at 16:12













          • i don't want second then, if i can get it with single then, that's ok for me

            – Jothi Kannan
            Jan 2 at 16:13











          • any other way @Robbie

            – Jothi Kannan
            Jan 2 at 16:27











          • async await is the way, did you see my udpated example?

            – Robbie Milejczak
            Jan 2 at 16:28



















          0














          For React app, most likely you will want to set the response as state:



          state = {
          thumbnailURL: ''
          }

          getThumbnail = (filePath) => {
          RNThumbnail.get(filePath)
          .then(response => response.path)
          .then(responseData => {
          this.setState({
          thumbnailURL: responseData
          })
          })
          .catch(error => console.warn(error))
          }

          render() {
          return (
          <img src={this.state.thumbnailURL} />
          )
          }


          You will need arrow function on getThumbnail for lexical binding so that you can access this.setState().





          Edit:



          You can't actually make getThumbnail() return thumbnailURL value right away. getThumbnail() can however return the promise, and you resolve it at the place where you want access to thumbnailURL:



          getThumbnail = filePath => {
          return RNThumbnail.get(filePath)
          .then(response => response.path)
          .then(responseData => responseData)
          .catch(error => console.warn(error))
          }

          IWannaAccessThumbnailURLHere = () => {
          this.getThumbnail('....')
          .then(thumbnailURL => {
          // do things with thumbnailURL
          })
          }


          Or, use setState, re-render then you can access this.state.thumbnailURL in the next render-cycle.






          share|improve this answer


























          • I don't want to store it in the state, instead i want to return it in a function

            – Jothi Kannan
            Jan 2 at 16:10











          • it's actually not possible to make getThumbnail() returns the value right away. Check my updated answer for other possibility.

            – Liren Yeo
            Jan 2 at 16:31











          • actually i want to loop in render to get a path of the stored video file, so this won't work, any other solution for my actual requirement?

            – Jothi Kannan
            Jan 2 at 16:38













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009166%2freact-native-get-response-from-then-and-assign-to-variable%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          .then doesn't work like that, it won't return a value. You could do:



            let thumbnailURL;

          RNThumbnail.get(filePath)
          .then((response) => response.path)
          .then((responseData) => {
          thumbnailURL = responseData;
          alert(thumbnailURL);
          }).catch(error => console.warn(error));


          but you have to continue computation inside the second then call because the value is only going to be reliable there



          You're better off using async/await, just refactor your code to this:



          async function getThumbnail(filePath){
          try {
          let thumbnailURL = await RNThumbnail.get(filePath)
          alert(thumbnailURL)
          } catch(err) {
          console.warn(err)
          }


          read more about async / await






          share|improve this answer


























          • tried 1st answer getting same undefined error, 2nd answer throwing error

            – Jothi Kannan
            Jan 2 at 16:11











          • well it's a little weird for you to resolve it twice, what is the purpose of your second .then call? I changed the second example, try that

            – Robbie Milejczak
            Jan 2 at 16:12













          • i don't want second then, if i can get it with single then, that's ok for me

            – Jothi Kannan
            Jan 2 at 16:13











          • any other way @Robbie

            – Jothi Kannan
            Jan 2 at 16:27











          • async await is the way, did you see my udpated example?

            – Robbie Milejczak
            Jan 2 at 16:28
















          3














          .then doesn't work like that, it won't return a value. You could do:



            let thumbnailURL;

          RNThumbnail.get(filePath)
          .then((response) => response.path)
          .then((responseData) => {
          thumbnailURL = responseData;
          alert(thumbnailURL);
          }).catch(error => console.warn(error));


          but you have to continue computation inside the second then call because the value is only going to be reliable there



          You're better off using async/await, just refactor your code to this:



          async function getThumbnail(filePath){
          try {
          let thumbnailURL = await RNThumbnail.get(filePath)
          alert(thumbnailURL)
          } catch(err) {
          console.warn(err)
          }


          read more about async / await






          share|improve this answer


























          • tried 1st answer getting same undefined error, 2nd answer throwing error

            – Jothi Kannan
            Jan 2 at 16:11











          • well it's a little weird for you to resolve it twice, what is the purpose of your second .then call? I changed the second example, try that

            – Robbie Milejczak
            Jan 2 at 16:12













          • i don't want second then, if i can get it with single then, that's ok for me

            – Jothi Kannan
            Jan 2 at 16:13











          • any other way @Robbie

            – Jothi Kannan
            Jan 2 at 16:27











          • async await is the way, did you see my udpated example?

            – Robbie Milejczak
            Jan 2 at 16:28














          3












          3








          3







          .then doesn't work like that, it won't return a value. You could do:



            let thumbnailURL;

          RNThumbnail.get(filePath)
          .then((response) => response.path)
          .then((responseData) => {
          thumbnailURL = responseData;
          alert(thumbnailURL);
          }).catch(error => console.warn(error));


          but you have to continue computation inside the second then call because the value is only going to be reliable there



          You're better off using async/await, just refactor your code to this:



          async function getThumbnail(filePath){
          try {
          let thumbnailURL = await RNThumbnail.get(filePath)
          alert(thumbnailURL)
          } catch(err) {
          console.warn(err)
          }


          read more about async / await






          share|improve this answer















          .then doesn't work like that, it won't return a value. You could do:



            let thumbnailURL;

          RNThumbnail.get(filePath)
          .then((response) => response.path)
          .then((responseData) => {
          thumbnailURL = responseData;
          alert(thumbnailURL);
          }).catch(error => console.warn(error));


          but you have to continue computation inside the second then call because the value is only going to be reliable there



          You're better off using async/await, just refactor your code to this:



          async function getThumbnail(filePath){
          try {
          let thumbnailURL = await RNThumbnail.get(filePath)
          alert(thumbnailURL)
          } catch(err) {
          console.warn(err)
          }


          read more about async / await







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 16:12

























          answered Jan 2 at 16:00









          Robbie MilejczakRobbie Milejczak

          2,8861727




          2,8861727













          • tried 1st answer getting same undefined error, 2nd answer throwing error

            – Jothi Kannan
            Jan 2 at 16:11











          • well it's a little weird for you to resolve it twice, what is the purpose of your second .then call? I changed the second example, try that

            – Robbie Milejczak
            Jan 2 at 16:12













          • i don't want second then, if i can get it with single then, that's ok for me

            – Jothi Kannan
            Jan 2 at 16:13











          • any other way @Robbie

            – Jothi Kannan
            Jan 2 at 16:27











          • async await is the way, did you see my udpated example?

            – Robbie Milejczak
            Jan 2 at 16:28



















          • tried 1st answer getting same undefined error, 2nd answer throwing error

            – Jothi Kannan
            Jan 2 at 16:11











          • well it's a little weird for you to resolve it twice, what is the purpose of your second .then call? I changed the second example, try that

            – Robbie Milejczak
            Jan 2 at 16:12













          • i don't want second then, if i can get it with single then, that's ok for me

            – Jothi Kannan
            Jan 2 at 16:13











          • any other way @Robbie

            – Jothi Kannan
            Jan 2 at 16:27











          • async await is the way, did you see my udpated example?

            – Robbie Milejczak
            Jan 2 at 16:28

















          tried 1st answer getting same undefined error, 2nd answer throwing error

          – Jothi Kannan
          Jan 2 at 16:11





          tried 1st answer getting same undefined error, 2nd answer throwing error

          – Jothi Kannan
          Jan 2 at 16:11













          well it's a little weird for you to resolve it twice, what is the purpose of your second .then call? I changed the second example, try that

          – Robbie Milejczak
          Jan 2 at 16:12







          well it's a little weird for you to resolve it twice, what is the purpose of your second .then call? I changed the second example, try that

          – Robbie Milejczak
          Jan 2 at 16:12















          i don't want second then, if i can get it with single then, that's ok for me

          – Jothi Kannan
          Jan 2 at 16:13





          i don't want second then, if i can get it with single then, that's ok for me

          – Jothi Kannan
          Jan 2 at 16:13













          any other way @Robbie

          – Jothi Kannan
          Jan 2 at 16:27





          any other way @Robbie

          – Jothi Kannan
          Jan 2 at 16:27













          async await is the way, did you see my udpated example?

          – Robbie Milejczak
          Jan 2 at 16:28





          async await is the way, did you see my udpated example?

          – Robbie Milejczak
          Jan 2 at 16:28













          0














          For React app, most likely you will want to set the response as state:



          state = {
          thumbnailURL: ''
          }

          getThumbnail = (filePath) => {
          RNThumbnail.get(filePath)
          .then(response => response.path)
          .then(responseData => {
          this.setState({
          thumbnailURL: responseData
          })
          })
          .catch(error => console.warn(error))
          }

          render() {
          return (
          <img src={this.state.thumbnailURL} />
          )
          }


          You will need arrow function on getThumbnail for lexical binding so that you can access this.setState().





          Edit:



          You can't actually make getThumbnail() return thumbnailURL value right away. getThumbnail() can however return the promise, and you resolve it at the place where you want access to thumbnailURL:



          getThumbnail = filePath => {
          return RNThumbnail.get(filePath)
          .then(response => response.path)
          .then(responseData => responseData)
          .catch(error => console.warn(error))
          }

          IWannaAccessThumbnailURLHere = () => {
          this.getThumbnail('....')
          .then(thumbnailURL => {
          // do things with thumbnailURL
          })
          }


          Or, use setState, re-render then you can access this.state.thumbnailURL in the next render-cycle.






          share|improve this answer


























          • I don't want to store it in the state, instead i want to return it in a function

            – Jothi Kannan
            Jan 2 at 16:10











          • it's actually not possible to make getThumbnail() returns the value right away. Check my updated answer for other possibility.

            – Liren Yeo
            Jan 2 at 16:31











          • actually i want to loop in render to get a path of the stored video file, so this won't work, any other solution for my actual requirement?

            – Jothi Kannan
            Jan 2 at 16:38


















          0














          For React app, most likely you will want to set the response as state:



          state = {
          thumbnailURL: ''
          }

          getThumbnail = (filePath) => {
          RNThumbnail.get(filePath)
          .then(response => response.path)
          .then(responseData => {
          this.setState({
          thumbnailURL: responseData
          })
          })
          .catch(error => console.warn(error))
          }

          render() {
          return (
          <img src={this.state.thumbnailURL} />
          )
          }


          You will need arrow function on getThumbnail for lexical binding so that you can access this.setState().





          Edit:



          You can't actually make getThumbnail() return thumbnailURL value right away. getThumbnail() can however return the promise, and you resolve it at the place where you want access to thumbnailURL:



          getThumbnail = filePath => {
          return RNThumbnail.get(filePath)
          .then(response => response.path)
          .then(responseData => responseData)
          .catch(error => console.warn(error))
          }

          IWannaAccessThumbnailURLHere = () => {
          this.getThumbnail('....')
          .then(thumbnailURL => {
          // do things with thumbnailURL
          })
          }


          Or, use setState, re-render then you can access this.state.thumbnailURL in the next render-cycle.






          share|improve this answer


























          • I don't want to store it in the state, instead i want to return it in a function

            – Jothi Kannan
            Jan 2 at 16:10











          • it's actually not possible to make getThumbnail() returns the value right away. Check my updated answer for other possibility.

            – Liren Yeo
            Jan 2 at 16:31











          • actually i want to loop in render to get a path of the stored video file, so this won't work, any other solution for my actual requirement?

            – Jothi Kannan
            Jan 2 at 16:38
















          0












          0








          0







          For React app, most likely you will want to set the response as state:



          state = {
          thumbnailURL: ''
          }

          getThumbnail = (filePath) => {
          RNThumbnail.get(filePath)
          .then(response => response.path)
          .then(responseData => {
          this.setState({
          thumbnailURL: responseData
          })
          })
          .catch(error => console.warn(error))
          }

          render() {
          return (
          <img src={this.state.thumbnailURL} />
          )
          }


          You will need arrow function on getThumbnail for lexical binding so that you can access this.setState().





          Edit:



          You can't actually make getThumbnail() return thumbnailURL value right away. getThumbnail() can however return the promise, and you resolve it at the place where you want access to thumbnailURL:



          getThumbnail = filePath => {
          return RNThumbnail.get(filePath)
          .then(response => response.path)
          .then(responseData => responseData)
          .catch(error => console.warn(error))
          }

          IWannaAccessThumbnailURLHere = () => {
          this.getThumbnail('....')
          .then(thumbnailURL => {
          // do things with thumbnailURL
          })
          }


          Or, use setState, re-render then you can access this.state.thumbnailURL in the next render-cycle.






          share|improve this answer















          For React app, most likely you will want to set the response as state:



          state = {
          thumbnailURL: ''
          }

          getThumbnail = (filePath) => {
          RNThumbnail.get(filePath)
          .then(response => response.path)
          .then(responseData => {
          this.setState({
          thumbnailURL: responseData
          })
          })
          .catch(error => console.warn(error))
          }

          render() {
          return (
          <img src={this.state.thumbnailURL} />
          )
          }


          You will need arrow function on getThumbnail for lexical binding so that you can access this.setState().





          Edit:



          You can't actually make getThumbnail() return thumbnailURL value right away. getThumbnail() can however return the promise, and you resolve it at the place where you want access to thumbnailURL:



          getThumbnail = filePath => {
          return RNThumbnail.get(filePath)
          .then(response => response.path)
          .then(responseData => responseData)
          .catch(error => console.warn(error))
          }

          IWannaAccessThumbnailURLHere = () => {
          this.getThumbnail('....')
          .then(thumbnailURL => {
          // do things with thumbnailURL
          })
          }


          Or, use setState, re-render then you can access this.state.thumbnailURL in the next render-cycle.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 16:30

























          answered Jan 2 at 16:06









          Liren YeoLiren Yeo

          1,1651520




          1,1651520













          • I don't want to store it in the state, instead i want to return it in a function

            – Jothi Kannan
            Jan 2 at 16:10











          • it's actually not possible to make getThumbnail() returns the value right away. Check my updated answer for other possibility.

            – Liren Yeo
            Jan 2 at 16:31











          • actually i want to loop in render to get a path of the stored video file, so this won't work, any other solution for my actual requirement?

            – Jothi Kannan
            Jan 2 at 16:38





















          • I don't want to store it in the state, instead i want to return it in a function

            – Jothi Kannan
            Jan 2 at 16:10











          • it's actually not possible to make getThumbnail() returns the value right away. Check my updated answer for other possibility.

            – Liren Yeo
            Jan 2 at 16:31











          • actually i want to loop in render to get a path of the stored video file, so this won't work, any other solution for my actual requirement?

            – Jothi Kannan
            Jan 2 at 16:38



















          I don't want to store it in the state, instead i want to return it in a function

          – Jothi Kannan
          Jan 2 at 16:10





          I don't want to store it in the state, instead i want to return it in a function

          – Jothi Kannan
          Jan 2 at 16:10













          it's actually not possible to make getThumbnail() returns the value right away. Check my updated answer for other possibility.

          – Liren Yeo
          Jan 2 at 16:31





          it's actually not possible to make getThumbnail() returns the value right away. Check my updated answer for other possibility.

          – Liren Yeo
          Jan 2 at 16:31













          actually i want to loop in render to get a path of the stored video file, so this won't work, any other solution for my actual requirement?

          – Jothi Kannan
          Jan 2 at 16:38







          actually i want to loop in render to get a path of the stored video file, so this won't work, any other solution for my actual requirement?

          – Jothi Kannan
          Jan 2 at 16:38




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009166%2freact-native-get-response-from-then-and-assign-to-variable%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'