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
createAd
function invokes createAd
function should callstoreData(data)
function where data stores in DB- after storing the data with function
bucket.insert
bucket.insert
callback return the valueerror
andresult
storeData
should return thebucket.insert
callback values tocreateAd
createAd
return value should contians thestoreData
return 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