How I can return value from callback function?
The code
function storeData(data) {
const id = "5f354b7470e79f7e5b6feb25";
const body = { name: "john doe" };
bucket.insert(id, body, (error, result) => {
console.log(error, result); // i want to return error & result from this callback
});
}
export default {
async createAd(_, { data }, { request }) {
try {
const dataa = await storeData(data);
return; // to here
} catch (error) {
console.log(error);
}
},
};
Here I need to return the value(error, result) comes from bucket.insert callback to function createAd and return that value from there.
bucket.insert --> createAd
How should it actually work
- when
createAdfunction invokes createAdfunction should callstoreData(data)function where data stores in DB- after storing the data with function
bucket.insert bucket.insertcallback return the valueerrorandresultstoreDatashould return thebucket.insertcallback values tocreateAdcreateAdreturn value should contians thestoreDatareturn value
bucket.insert should contain a callback else it throws an error Third argument needs to be an object or callback.
solution tried
- promisifing bucket.insert with nodejs util.promisify