0

util.promisify appears to always expect 2 parameters from a callback function, however the existing functions do not have a seperate callback for err and data, but only a single callback.

How can I handle this ???

const {promisify} = require('util');

function y(text,cb){
    setTimeout(function(){cb({error:false,data:text})},1000);   
}

async function test(text) {
  try{
    const z = promisify(y)
    return await z(text);
  } catch(e) {return {error:true,msg:e}} 
} 

console.log(test('xxx'));

What I am looking for is to return the value from function y syncronously and not getting a promise i.e.

var x = test('xxx');
crankshaft
  • 2,607
  • 4
  • 45
  • 77
  • 1
    So what happens normally when there's an error in `y` (without promisify?) – CertainPerformance Dec 14 '18 at 02:15
  • `return await new Promise(resolve => y(q, resolve));` – Felix Kling Dec 14 '18 at 02:22
  • If your callback only accepts one parameter then how do you know when there's an error? – Patrick Roberts Dec 14 '18 at 02:30
  • @PatrickRoberts - because in the real function it always replys with {error:true/false, data:....} - this is just a simple test to figure out the syntax :-) – crankshaft Dec 14 '18 at 02:32
  • @FelixKling - this returns a promise, but what I am looking for is to return the result and not a promise. – crankshaft Dec 14 '18 at 05:06
  • An async function always returns a promise. If you have a promise/async function anywhere in the call stack, it’s promises/async all the way up. You cannot eat the pizza before it was delivered. What you can do however is `(async function() { var x = await test('xxx'); }())` – Felix Kling Dec 14 '18 at 05:49

1 Answers1

0

Given the information in your comment, you can wrap the function with a compatible signature to be passed directly to promisify():

const { promisify } = require('util')

function y (query, callback) {
  callback(query)   
}

function yCompatible (query, callback) {
  y(query, ({ error, data }) => {
    callback(error && data, error || data)
  })
}

const yAsync = promisify(yCompatible)

async function test (query) {
  try {
    return yAsync(query)
  } catch (error) {
    return error
  } 
} 

test('xxx').then(
  data => { console.log(data) },
  error => { console.error(error) }
)

Also try not to get in the habit of using single letter variables like a mathematician ;) I realize this is just example code, but even then it's helpful to be a bit more explicit about your intent.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • Thanks, but the problem is this is returning a promise, but I am looking to return the value returned by the y callback and not a promise as that is still not sync. – crankshaft Dec 14 '18 at 05:15
  • 1
    @crankshaft that's impossible. A callback and a promise are two methods of handling data that will be available _in the future_. You can't convert a representation of a future value into the value itself, you still have to wait for it at some point. That's why you can't just `return` it. See [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Roberts Dec 14 '18 at 05:39