I am trying to get the result of an async function in my calculateOrderAmount function but it returns undefined.
The console.log in the called function returns the good result, but inside calculateOrderAmount, I get undefined. Here is my code:
getMultiStrats = async () => {
await MultiStrats.findOne({}, (err, multiStrats) => {
if (err) {
return err
}
if(!multiStrats) {
return console.log('MultiStrat not found')
}
console.log('returns MultiStrat: ' + multiStrats)
return multiStrats
})
.catch(err => console.log(err))
}
async function calculateOrderAmount(balance, ticker){
const multiState = await StrategyController.getMultiStrats().catch((err) => console.log(err))
console.log('multiState: ' + multiState)
some logic
}
Here is the console log:
multiState: undefined
returns MultiStrat: {
_id: 5ff73c74d1135b39fc709b80,
positionsCount: 1,
inTradeCount: 0,
__v: 0
}
What did I miss? Thanks you very much for your time!
The current approach is pretty unclear--there's no need for .catch, async, await all at once. return multiStrats returns from the inside of the callback, not from getMultiStrats. The async/await on getMultiStrats is superfluous, just adding another promise wrapper that doesn't accomplish anything.
Given that findOne as shown here uses a callback rather than a promise, you can either use callbacks all the way or you can promisify findOne as follows, using .then and .catch in the caller:
const MultiStrats = {
findOne: (obj, cb) => cb(null, "I'm a multistrat!")
};
const StrategyController = {
getMultiStrats: () => new Promise((resolve, reject) =>
MultiStrats.findOne({}, (err, multiStrats) => {
if (err) {
return reject(err);
}
else if (multiStrats) {
return resolve(multiStrats);
}
reject(Error("Multistrat not found"));
})
)
};
const calculateOrderAmount = (balance, ticker) =>
StrategyController
.getMultiStrats()
.then(multiState => {
console.log('multiState: ' + multiState)
// some logic
})
.catch(err => console.error(err))
;
calculateOrderAmount();
Or use async/await and try/catch:
const MultiStrats = {
findOne: (obj, cb) => cb(null, "I'm a multistrat!")
};
const StrategyController = {
getMultiStrats: () => new Promise((resolve, reject) =>
MultiStrats.findOne({}, (err, multiStrats) => {
if (err) {
return reject(err);
}
else if (multiStrats) {
return resolve(multiStrats);
}
reject(Error("Multistrat not found"));
})
)
};
const calculateOrderAmount = async (balance, ticker) => {
try {
const multiState = await StrategyController.getMultiStrats();
console.log('multiState: ' + multiState)
// some logic
}
catch (err) {
console.error(err);
}
};
calculateOrderAmount();
If this is MongoDB's findOne and already returns a promise, then you can just return the promise to the caller, optionally awaiting it and throwing for the null result:
const MultiStrats = {
findOne: async query => "I'm a multistrat!"
};
const StrategyController = {
getMultiStrats: async () => {
const result = await MultiStrats.findOne({});
if (result) {
return result;
}
throw Error("Multistrat not found");
}
};
const calculateOrderAmount = (balance, ticker) =>
StrategyController
.getMultiStrats()
.then(multiState => {
console.log('multiState: ' + multiState);
// some logic
})
.catch(err => console.error(err))
;
calculateOrderAmount();
You cannot return values from an inner callback and reach the outer function, I would suggest
1- only use promises
2- wrap your code with promise to be sure that await will return the async result the way you expect to have.
getMultiStrats = async () => {
return new Promise((resolve, reject) => {
MultiStrats.findOne({}, (err, multiStrats) => {
if (err) {
return err
}
if (!multiStrats) {
console.log('MultiStrat not found')
reject('MultiStrat not found')
}
console.log('returns MultiStrat: ' + multiStrats)
resolve(multiStrats);
})
.catch(err => {
console.log(err);
reject(err)
})
})
}
async function calculateOrderAmount(balance, ticker) {
try {
const multiState = await StrategyController.getMultiStrats()
console.log('multiState: ' + multiState)
// some logic
} catch (error) {
console.error(error);
}
}
Assuming that you use mongoose. I suggest using the promise interface like described in the documentation.
const getMultiStrats = async () => {
const query = MultiStrats.findOne({});
let multiStrats;
try {
multiStrats = await query.exec();
} catch (error) {
return error;
}
if (multiStrats) {
console.log("returns MultiStrat: " + multiStrats);
} else {
console.log("MultiStrat not found");
}
return multiStrats;
}
I would personally not return the error, but instead just let the error be thrown. With the above code the caller of getMultiStrats has to figure out if there return value is the expected result or an error. If you don't catch the error, it is thrown further up to the caller.
const getMultiStrats = async () => {
const multiStrats = await MultiStrats.findOne({}).exec();
if (multiStrats) {
console.log("returns MultiStrat: " + multiStrats);
} else {
console.log("MultiStrat not found");
}
return multiStrats;
}
You can further simplify this if you where to leave the console.log of of the equation.
const getMultiStrats = () => MultiStrats.findOne({}).exec();
Related
I don't know why i am getting this error
uncaughtException: await is only valid in async function
as i am using async keyword at getChild function still i am getting this error
my code:
async function filterFile(folderPath) {
try {
wantExt = [".jpg"];
let parts;
const paths = await checkFileLoc(folderPath, 3);
const otherFiles = [];
for (const filePath of paths) {
parts = filePath.split("/");
let splitFileName = parts[parts.length - 1].split(".");
if (wantExt.includes(`.${splitFileName[splitFileName.length - 1]}`)) {
otherFiles.push(filePath);
}
}
let ignoreFile = otherFiles.filter((x) =>
x.endsWith("_bio.jpg")
);
let test = otherFiles.filter((x) => !ignoreZipFile.includes(x));
return { test };
} catch (error) {
console.log("error:", error);
}
}
async function getChild(parents) {
return new Promise(function (resolve, reject) {
Shop.findAll({
where: {
shop_no: parents.id,
},
attributes: ["id", "name"],
})
.then((children) => {
let gotValue= await filterFile(children);
console.log(gotValue);
resolve(children);
})
.catch(function (err) {
reject(err);
});
});
}
and if i remove async from the function then I am getting gotValue as promise don't know how to get value
Try adding async to callback function in findAll
async function getChild(parents) {
return new Promise(function (resolve, reject) {
Shop.findAll({
where: {
shop_no: parents.id,
},
attributes: ["id", "name"],
})
.then(async (children) => { // Add it here
let gotValue= await filterFile(children);
console.log(gotValue);
resolve(children);
})
.catch(function (err) {
reject(err);
});
});
}
You missed async keyword before
(children) => {
let gotValue = await filterFile(children);
console.log(gotValue);
resolve(children);
}
function. It should be
async (children) => {
let gotValue = await filterFile(children);
console.log(gotValue);
resolve(children);
}
"await is only valid in async function"
Exactly as the error says, await is used inside an async function.
const fn = async () => { await someAsyncFunction() };
So in your scenario, make the callback async as well to await the filterFile code.
.then(async (children) => {
let gotValue= await filterFile(children);
console.log(gotValue);
resolve(children);
})
async function filterFile(folderPath) {
try {
wantExt = [".jpg"];
let parts;
const paths = await checkFileLoc(folderPath, 3);
const otherFiles = [];
for (const filePath of paths) {
parts = filePath.split("/");
let splitFileName = parts[parts.length - 1].split(".");
if (wantExt.includes(`.${splitFileName[splitFileName.length - 1]}`)) {
otherFiles.push(filePath);
}
}
let ignoreFile = otherFiles.filter((x) =>
x.endsWith("_bio.jpg")
);
let test = otherFiles.filter((x) => !ignoreZipFile.includes(x));
return { test };
} catch (error) {
console.log("error:", error);
}
}
async function getChild(parents) {
try {
const children = await Shop.findAll({
where: {
shop_no: parents.id,
},
attributes: ["id", "name"],
});
let gotValue= await filterFile(children);
console.log(gotValue);
return children;
} catch(err) {
throw(err);
}
}
I am using a library that calls an API, and I am waiting for Promise return to receive an Array.
However, even though I expect 2 elements in ActivityItem array, sometimes I receive only first of them (the one that appears first (Item1). From my point of view, I implemented the Promise incorrectly and there should be a mistake in the way I return them, but I miss seeing it.
Here I call the function that uses that should return Promise:
componentDidMount() {
this.getDataFromKit(ONEDAYINTERVAL).then(result => {
this.sendDataToServer(result); //sending to backend
}).catch(e => console.error);
}
And here is a method itself:
getDataFromKit(dateFrom) {
return new Promise((resolve) => {
AppleKit.initKit(KitPermissions.uploadBasicKitData(), (err, results) => {
if (err) {
return;
}
AppleKit.getSamples(dateFrom, (err, results) => {
if (err) {
return resolve([]);
}
const newData = results.map(item => {
return { ...item, name: "Item1" };
});
const allData = [...this.state.ActivityItem, ...newData];
this.setState({ ActivityItem: allData });
resolve(allData);
});
// if I delete the code below it will work just fine always grabbing only one item.
new Promise((resolve) => {
AppleKit.getSamplesSecondMethod(dateFrom, (err, results) => {
if (err) {
return resolve([]);
}
const newData = results.map(item => {
return { ...item, name: "Item2" };
});
const allData = [...this.state.ActivityItem, ...newData];
this.setState({ ActivityItem: allData });
resolve(allData);
});
});
});
})
}
The main issue here is I guess: how can I return multiple promises from this one method?
The problem as I see it, is your second block of code doesn't get run because you are resolving the promise in the first block. The way you have it coded, you will want to resolve that promise only once all your async operations have completed. I modified your code, but have not tested it. It may need to add the .then method to make sure the async data is returned before resolving the initial promise.
What happens if you try this ?
UPDATE
It looks like the below code solved your problem as you accepted my answer. However, I did re-write it before I realized you accepted so I will add the new updated code in case that will help you or someone else.
Original Answer
getDataFromKit(dateFrom) {
const thenable = new Promise((resolve) => {
AppleKit.initKit(KitPermissions.uploadBasicKitData(), (err, results) => {
if (err) {
return;
}
AppleKit.getSamples(dateFrom, (err, results) => {
if (err) {
return resolve([]);
}
const newData = results.map(item => {
return {
...item,
name: "Item1"
};
});
resolve(newData);
});
});
})
.then((newData) => {
AppleKit.initKit(KitPermissions.uploadBasicKitData(), (err, results) => {
if (err) {
return;
}
AppleKit.getSamplesSecondMethod(dateFrom, (err, results) => {
if (err) {
return;
}
var stateData = this.state.ActivityItem;
const addData = results.map(item => {
return {
...item,
name: "Item2"
};
});
stateData = [...stateData, ...newData];
stateData = [...stateData, ...addData];
this.setState({
ActivityItem: stateData
});
});
});
});
return thenable;
}
Updated Code using Promise.all
getDataFromKit(dateFrom) {
return new Promise((resolve) => {
const promise1 = new Promise((resolve) => {
AppleKit.initKit(KitPermissions.uploadBasicKitData(), (err, results) => {
if (err) {
return Promise.reject(err);
}
AppleKit.getSamples(dateFrom, (err, results) => {
if (err) {
return Promise.reject(err);
}
const newData = results.map(item => {
return {
...item,
name: "Item1"
};
});
return Promise.resolve(newData);
});
});
});
const promise2 = new Promise((resolve) => {
AppleKit.initKit(KitPermissions.uploadBasicKitData(), (err, results) => {
if (err) {
return Promise.reject(err);
}
AppleKit.getSamplesSecondMethod(dateFrom, (err, results) => {
if (err) {
return Promise.reject(err);
}
const moreData = results.map(item => {
return {
...item,
name: "Item2"
};
});
return Promise.resolve(moreData);
});
});
});
Promise.all([promise1, promise2])
.then(([result1, result2]) => {
var nArrays = [result1, result2, this.state.ActivityItem];
const finalResult = [].concat(...nArrays);
return Promise.resolve(finalResult);
});
});
}
I have one function, which returns Promise:
updatePassword(currentPassword: string, newPassword: string): Promise<void> {
return this.fireAuth.currentUser.then((user: firebase.User) => {
if (user) {
const credentials = auth.EmailAuthProvider.credential(user.email, currentPassword)
user.reauthenticateWithCredential(credentials).then(res => {
if (res) {
user.updatePassword(newPassword)
}
}).catch(err => {
throw new Error(err)
})
}
})
}
I call it inside another component:
this.userService.updatePassword(currentPassword, newPassword).then(() => {
console.log('successfully')
}).catch(err => {
console.log('error')
})
But even when updatePassword() return Error, the function call in component still console log 'successfully' from 'then`. How to properly throw an error in my case?
You also need to return your inner promise. Like this:
updatePassword(currentPassword: string, newPassword: string): Promise<void> {
return this.fireAuth.currentUser.then((user: firebase.User) => {
if (user) {
const credentials = auth.EmailAuthProvider.credential(user.email, currentPassword)
return user.reauthenticateWithCredential(credentials).then(res => {
if (res) {
user.updatePassword(newPassword)
}
}).catch(err => {
throw new Error(err)
})
}
throw new Error('USER_NOT_FOUND')
})
}
Edit:
I also added throw if you do not get user as safety net.
Refactor your function to use async/await, and you don't need to manually throw anything.
You may wish to make those return falses some sort of throw too.
async updatePassword(currentPassword: string, newPassword: string): Promise<void> {
const user = await this.fireAuth.currentUser;
if(!user) return false;
const credentials = auth.EmailAuthProvider.credential(user.email, currentPassword);
const res = await user.reauthenticateWithCredential(credentials);
if(!res) return false;
user.updatePassword(newPassword);
return true;
}
If your fireAuth call fails, then throw a Error from the updatePassword catch block.
Check the working snippet attached.
let fireAuth = (pass = false) => {
if (pass) return Promise.resolve("passed");
return Promise.reject("failed");
};
function updatePassword(shouldPass = false) {
if (!shouldPass) {
return fireAuth(false)
.then(console.log)
.catch((err) => {
console.log('err in fireauth', err)
throw new Error('OOPS')
});
}
return Promise.resolve("success");
}
let failedResult = updatePassword()
.then()
.catch((err) => console.log("failedresult error", err.toString()));
let successResult = updatePassword(true)
.then((res) => console.log("res is", res))
.catch();
I am trying to refactor this code using try-catch blocks:
export const authorizeConnectyCube = async (accessToken) => {
const userCredentials = {
provider: 'firebase_phone',
'firebase_phone[project_id]': "xxxxxxxx",
'firebase_phone[access_token]': accessToken,
};
await createSession();
return new Promise((resolve, reject) => {
ConnectyCube.login(userCredentials, (error, user) => {
user ? resolve(user) : reject(error);
})
}).catch(error => console.log(error));
}
const createSession = () => {
return new Promise((resolve, reject) => {
ConnectyCube.createSession((error, session) => {
session ? resolve(session.user) : reject(error)
})
}).catch(error => console.log(error));
}
However I'm not getting the same result - the asynchronousity seems to be being handled differently. Here is my attempt at refactoring:
export const authorizeConnectyCube = async (accessToken) => {
const userCredentials = {
provider: 'firebase_phone',
'firebase_phone[project_id]': "xxxxxxxxxx",
'firebase_phone[access_token]': accessToken,
};
await createSession();
try {
ConnectyCube.login(userCredentials, (error, user) => {
return user;
})
}
catch (error) {
console.log(error)
}
}
const createSession = () => {
try {
ConnectyCube.createSession((error, session) => {
return session.user
})
} catch (error) {
console.log(error);
}
}
Is there any particular part of what I'm wrong? Thanks.
Callback-based APIs don't readily turn into something you can use for async/await (which under the hood uses promises). You'll have to "promisify" them first (i.e. wrap them in promises).
Here's an example of what I'm trying to say:
// Promisify these callback-based APIs.
const login = userCredentials => {
return new Promise((resolve, reject) => {
ConnectyCube.login(userCredentials, (error, user) => {
user ? resolve(user) : reject(error);
})
})
})
const createSession = () => {
return new Promise((resolve, reject) => {
ConnectyCube.createSession((error, session) => {
session ? resolve(session.user) : reject(error)
})
})
})
// Then use them in an async function
export const authorizeConnectyCube = async (accessToken) => {
const userCredentials = {
provider: 'firebase_phone',
'firebase_phone[project_id]': "xxxxxxxx",
'firebase_phone[access_token]': accessToken,
}
try {
await createSession()
return login(userCredentials)
} catch (e) {
console.warn(e)
}
}
Also, async functions return promises, with the resolved value being the return value, and the rejected value being any uncaught error thrown inside. A value wrapped in a promise as return value for an async function is redundant.
If you're using Node 8+, it has a utility called promisify which accepts a callback-based API and returns a promise-returning version of it.
How come I do not get a value in my res after the promise resolves?
My console.log looks something like this:
=====!!USER NOT FOUND!!=====
Res: undefined
This is my function
async function findUser(userID, userType) {
await new Promise((resolve, reject) => {
usersTable.findOne (
{ _id: userID }
,function (err, data) {
if (err) {
throw new Error('findUser: ' + err);
} else {
if (!data) {
console.log("=====!!USER NOT FOUND!!=====")
resolve("NEW");
} else {
console.log("=====USER FOUND=====")
resolve("OK");
};
};
});
})};
This is my caller
async function main() {
var res = "";
try {
// Find the user
res = await findUser(userEmail, "tenant");
console.log("Res: " + res );
if (res == "NEW") {
res = await newUser(); // Add the new tenant
}
}
catch(err) {
console.error(err);
console.log(" newBooking: " + err);
callback( { error:true, err } );
}
}
main();
find User should return sth:
async function findUser(userID, userType) {
return await new Promise((resolve, reject) => {
...
});
}
If you do await findUser() youre waiting until the async function returns sth. Your async function returns undefined.
By the way: You might not use an async function at all:
function findUser(userId,userType){
return new Promise(...);
}