-1

This is a follow-up question to What is wrong with this code that promisify a function?

Method 1 works;

var Converter = require('csvtojson').Converter;
Promise.promisifyAll(Converter.prototype);
var converter = new Converter();

Method 2 does not work;

var Converter = require('csvtojson').Converter;
var converter = Promise.promisifyAll(Converter.prototype);

Why does method 1 work and not method 2?

Community
  • 1
  • 1
guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • `var Converter = require('csvtojson').Converter; var converter = new Converter(); Promise.promisifyAll(Converter.prototype);` will probably work too, promisifyAll is adding new methods to the Converter objects, those methods ends with Async http://bluebirdjs.com/docs/api/promise.promisifyall.html (if you're using bluebird) – caub Nov 07 '15 at 11:25
  • you mean you need to do: `var converter = new Promise.promisifyAll(Converter.prototype);`? – caub Nov 07 '15 at 11:30
  • @crl, just tested that. No, that does not work. – guagay_wk Nov 07 '15 at 11:38
  • what about `var csvtojson = require('csvtojson'); Promise.promisifyAll(csvtojson); var converter = csvtojson.Converter(); converter.doSomethingAsync().then(...)` ? – caub Nov 07 '15 at 11:42
  • It doesn't work because you're not creating a new converter in the second method. There's no "error" because there's no reason you can't set a variable to any type you want. – Dave Newton Nov 07 '15 at 12:19
  • 1
    (I mean, the question kind of answers itself without any re-examining of docs necessary--the code as shown in the question is about as clear as you can get. If the first one works it's pretty obvious you have to use `new`, no?) – Dave Newton Nov 07 '15 at 12:31
  • @ Dave Newton, it wasn't obvious to me when I asked the question. Not as advanced as you:) I guess I answered my own question later with the EDIT. I will just answer the question anyway. – guagay_wk Nov 07 '15 at 12:36
  • May I ask why the negative vote? Stupid question? Please explain so that I can improve my questions in future or should I just delete the question? – guagay_wk Nov 07 '15 at 12:37
  • 1
    Try `var converter = new (Promise.promisifyAll(require('csvtojson').Converter.prototype).constructor);` – Bergi Nov 07 '15 at 12:54
  • @Bergi, nice one. May I roll that into my answer please? – Roamer-1888 Nov 07 '15 at 13:15
  • 2
    @Roamer-1888: Nah, it's a horrible one-liner that should be 3 lines (like "Method 1"). Also Bluebird should be capable of promisifying complete modules, so idiomatic would be `var converter = new (Promise.promisifyAll(require('csvtojson')).Converter);` – Bergi Nov 07 '15 at 13:19
  • @Roamer-1888: … or even `var converter = Promise.promisifyAll(new (require('csvtojson').Converter));` – Bergi Nov 07 '15 at 13:21
  • @Bergi, Last version does something slightly different - it promisifies the instance not the constructor. Further instances would need to be promisifed independently. – Roamer-1888 Nov 07 '15 at 13:27
  • @Roamer-1888: Afaik Bluebird does follow both the prototype chain and all class properties, so the result should be the same – Bergi Nov 07 '15 at 13:31
  • @Bergi, yes agreed, result will be the same, just arrived at via a different route. – Roamer-1888 Nov 07 '15 at 13:34

2 Answers2

1
Promise.promisifyAll(obj)

returns obj, therefore ...

Promise.promisifyAll(Converter.prototype)

... returns Converter.prototype, not Converter therefore ...

var converter = Promise.promisifyAll(Converter.prototype);

... will assign Converter.prototype to converter.

In order to promisify the prototype and assign an instance of Converter, you should (realistically) write two statements (ie Method 1) :

Promise.promisifyAll(Converter.prototype);
var converter = new Converter();

You could write the single line ...

var converter = new (Promise.promisifyAll(Converter.prototype).constructor);

... though it's less readable.

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
0

On re-examination of the documentation https://www.npmjs.com/package/csvtojson, Convertor is a constructor. It has to be used with new in front.

guagay_wk
  • 26,337
  • 54
  • 186
  • 295