Extract internal variable from anonymous function [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
as the title says I want to extract the variable status to be able to use it in another part of the code, is this possible?
RNAudioStreamer.status((err, status)=>{if(!err) console.log(status)})

Depending on the callback sync/async you can create a global variable or use promise.
let p = new Promise((resolve, reject) => {
RNAudioStreamer.status((err, status)=>{
if(!err) resolve(status); else reject(err);
})
});
// ...
p.then(status => {
// process status here
})

Related

How can retrieve chrome.storage.local.get get value globaly? [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 5 months ago.
I want to get a value from chrome.storage.local
var storage = chrome.storage.local;
authToken = storage.get('logintoken', function(result) {
var channels = result.logintoken;
authToken=result.logintoken;
console.log(authToken);
return authToken;
});
alert(authToken);
but out of the function authToken is undefined.
function getAllStorageSyncData(top_key) {
// Immediately return a promise and start asynchronous work
return new Promise((resolve, reject) => {
// Asynchronously fetch all data from storage.sync.
chrome.storage.local.get(top_key, (items) => {
// Pass any observed errors down the promise chain.
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
// Pass the data retrieved from storage down the promise chain.
resolve(items);
});
});
}
Used this method and await like
var obj = await getAllStorageSyncData(['logintoken']);
and its worked
https://stackoverflow.com/a/72947864/1615818

NodeJS Function returning undefined [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I have a function that needs to get data from another function, but the second one is returning undefined and i don't know why.
function firstOne() {
const companys = companysList();
console.log(companys); // LOG UNDEFINED
};
.
function companysList(){
db.query('SELECT id FROM companys WHERE id > 0', (error, result) =>{
if (error) throw error;
console.log(JSON.stringify(result)); // LOG THE ids!
return result;
});
};
your companysList function doesn't return anything

sqlite3 db asynchronous problem in function [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I have a problem with function check_mod.
function check_mod(user_id) {
db.each(`SELECT mode FROM users WHERE id = ` + user_id, [], (err, row) => {
if (err) {
throw err;
}
if(row.mode == 1)
{
return true;
}
});
}
if(check_mod('286927644405137408')) console.log("okay");
When i try to use this function, it returns "undefined" and i don't know why. I think that i should use async and await but i don't know how to use it.
I'm not sure but, I do notice that there is no semicolon at the end of the sql statement which could be causing the issue.

Unable to save data from anonymous function [duplicate]

This question already has answers here:
How do I convert an existing callback API to promises?
(24 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I'm currently refactoring some code, and I'm running into a problem.
I am trying to return the value from inside an anonymous function. I played around with it a few different ways, and tried await-ing it.
Essentially I'm getting data about the request that's being made, not the data from the request.
Here are some different ways I've tried working with it -
const run = async (text) => {
...
const s = personalityInsights.profile(req, (err, data) => {
...
return data; // correct value
});
return s; // incorrect value
};
const run = async (text) => {
...
personalityInsights.profile(req, (err, data) => {
...
return data;
});
};
const run = async (text) => {
...
return personalityInsights.profile(req, (err, data) => {
...
return data;
});
};
I can give the full code, but I think this is more of a language specific problem I have, than a code specific one.
Thanks for your help,
Ollie
Edit: This question has been marked as a duplicate, which I understand - but the solutions in that duplicate simply aren't working, and they aren't things I haven't already tried...

Node.js MongoDB collection.find().toArray returns nothing [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 5 years ago.
Although I found similar questions to mine, I couldn't solve the problem on my own.
In my '../models/user' model I want to find all users and put them into array, and return that array to the controller(where I will use the info).
Here is my code:
var mongoDatabase = require('../db');
var database = mongoDatabase.getDb();
function find() {
var test;
database.collection("customers").find().toArray( function(err, docs) {
if(err) throw err;
console.log(docs); //works fine
//I'd like to return docs array to the caller
test = docs;
});
console.log(test); //test is undefined
}
module.exports = {
find
};
I also noticed, that 'console.log(test)' goes before 'console.log(docs)'. I tried passing 'docs' argument as function parameter to 'find', but without result.
The best way is to use Promises. Do it like this.
function getUsers () {
return new Promise(function(resolve, reject) {
database.collection("customers").find().toArray( function(err, docs) {
if (err) {
// Reject the Promise with an error
return reject(err)
}
// Resolve (or fulfill) the promise with data
return resolve(docs)
})
})
}

Categories