I am using a certain node.js library in which you define a callback function that needs to return a value.
Inside this callback I want to use a function from another library that returns a promise of that value.
How can I define the callback function to return the value from the resolved promise ?
How do I edit the following snippet so that I return the value to the callback only when then promise was resolved ?
Util_A.someFunction( (a)=>{
Util_B.somePromise(a)
.then((result)=>{
// This is the value that I want to return to
// the original function 'someFunction'
return result;
});
}),
I am using a certain node.js library in which you define a callback function that needs to return a value.
Inside this callback I want to use a function from another library that returns a promise of that value.
How can I define the callback function to return the value from the resolved promise ?
How do I edit the following snippet so that I return the value to the callback only when then promise was resolved ?
Util_A.someFunction( (a)=>{
Util_B.somePromise(a)
.then((result)=>{
// This is the value that I want to return to
// the original function 'someFunction'
return result;
});
}),
Share
Improve this question
edited Mar 16, 2019 at 16:44
Yaron
asked Mar 15, 2019 at 14:43
YaronYaron
1,9757 gold badges24 silver badges41 bronze badges
7
- Hi, Try to return the Util_B.sommePropose(a)....stuff – Maxime Girou Commented Mar 15, 2019 at 14:45
- 2 return the value to the callback - this sentence doesn't make sense. This isn't how promises and callbacks work. Please, provide more meaningful example that shows what your case is. – Estus Flask Commented Mar 15, 2019 at 14:46
-
What do you mean by "define the callback function to return the value"? Do you want to return it from the callback to the
Util_A
? What exactly isUtil_A.somecallback
? A more concrete example would make your question much easier to understand. – Bergi Commented Mar 15, 2019 at 14:46 -
I think he wants to wait for the result of the promise, get the value and return it as the return value of the callback function passed to
Util_A.somecallback
, but that's not how it is supposed work... – sjahan Commented Mar 15, 2019 at 14:47 - You can't do either of those things. You can't return the value from the result of the promise to the callback, and you can't a value from the callback to an outer scope. – Kevin B Commented Mar 15, 2019 at 15:14
3 Answers
Reset to default 2So if I understand correctly, somecallback
would do something with the return value from the callback it calls.
Unless somecallback
supports promises or other asynchronicity from the retval, there's no way to do this.
Maybe something like this:?
new Promise(resolve => Util_A.somecallback(resolve))
.then(a => Util_B.somePromise(a))
.then(result => console.log(result))
.catch (err => console.error(err.message));
EDIT: Better chaining of promises.
You should convert the Util_A
function into a Promise
based function. How ? Using the native Util.promisify() method of node.js (I've made a custom implementation for the snippet).
Never mix up callback/promise.
const Util_A = {
somecallback: callback => callback(false, 'callback return'),
};
const Util_B = {
somepromise: () => new Promise((resolve) => resolve('promise return')),
};
function promisify(func) {
return () => new Promise((resolve, reject) => {
func((err, val) => {
if (err) return reject(err);
return resolve(val);
});
});
}
// Native Util.promisify() in node.js
promisify(Util_A.somecallback)()
.then((ret) => {
console.log(ret);
return Util_B.somepromise();
})
.then((ret) => {
console.log(ret);
});