react native get response from then and assign to variable
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
add a comment |
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
add a comment |
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
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
reactjs react-native
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
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
.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
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.thencall? 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
add a comment |
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.
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 makegetThumbnail()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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
.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
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.thencall? 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
add a comment |
.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
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.thencall? 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
add a comment |
.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
.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
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.thencall? 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
add a comment |
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.thencall? 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
add a comment |
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.
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 makegetThumbnail()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
add a comment |
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.
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 makegetThumbnail()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
add a comment |
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.
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.
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 makegetThumbnail()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
add a comment |
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 makegetThumbnail()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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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