I know there's a ton of question similar to my question, but i didn't see any good case to help me,
I have a callback from native function bridge and this how i used it on JS:
getAllParameter((data)=>{
console.log(data) // data is Javascript Object
})
I've tried this to get the value of data:
getAllParameter((data)=>{
return new Promise((resolve)=> resolve(showToken(data.Token)))
})
async function showToken(token){
var res = await token
return res
}
var isiToken = showToken()
console.log("isiToken")
console.log(isiToken)
but the result is:
isiToken
{ _40: 0, _65: 0, _55: null, _72: null }
i don't know whats wrong with my code, i want to get the value of data outside of the getAllParameter, how can i do that properly?
the reason why i need to get the result of getAllParameter and used it freely is because I've token value inside the data, and i need to use the token in axios instance config
so the full code of my file should be:
getAllParameter((data)=>{
return new Promise((resolve)=> resolve(showToken(data.Token)))
})
async function showToken(token){
var res = await token
console.log("res")
console.log(res)
return res
}
var isiToken = showToken()
console.log("isiToken")
console.log(isiToken)
const http = Axios.create ({
baseURL: Constants.APILink,
timeout: Constants.Timeout,
headers: {'Content-Type': 'application/json', 'Authorization': 'bearer '+isiToken}
export default http
});
I am not sure of your getAllParameter definition but that method should be calling your callback at the end. Hoping that it does that, here is snippet that does what you want
(function() {
var data;
function getAllParam(callback) {
console.log("getAllParam");
callback("getAllParam");
}
getAllParam((data)=> {
this.data = data);
console.log(this.data);
});
})();
So, what I am doing is
But here is the limitation with my code: This doesn't work when getAllParam is an async function. Meaning if the callback is not called in sequence. You have to use promises then.
EDIT
app.js
function getAllParam(callback) {
console.log("getAllParam");
callback({Token: "getAllParam"});
}
var httpPromise = new Promise(resolve => {
getAllParam((data) => {
let token = data.Token;
console.log("Creating http from here using token");
let http = Axios.create({ bearer: token});
resolve(http);
})
});
export default httpPromise;
file_that_imports_app_js.js
import httpPromise from "./app.js";
async function init() {
let http = await httpPromise;
http.get("/", ...)
}
init();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With