0

I got a problem with a promise from openpgpjs. If you want to see the entire code : http://pastebin.com/dkcUQVBP (at line 16 and 157)

In NodeJS, i use it like that :

    var serverPrivateKey = null;
    var serverPublicKey  = null;
    var isGenerated = false;
    openpgp.generateKeyPair(options).then(function(keypair) {
        // Success
        serverPrivateKey = keypair.privateKeyArmored;
        serverPublicKey = keypair.publicKeyArmored;
        isGenerated = true;
       
    }).catch(function(error) {
        console.log("Erreur lors de la création des clés.");
    });

That part works fine, got no problem with it.

But, the same part don't work in angularjs, i got :

        io.pgp.generateKeyPair(io.options).then(function(keypair) {
            io.setClientPrivateKey(keypair.privateKeyArmored);
            io.setClientPublicKey(keypair.publicKeyArmored);
        });

I'm not really good with promise and i don't get the problem here.

Inside the promise, i can call a console.log(keypair.privateKeyArmored), and it's working fine. But when i try outside the promise, it's like the data never have been loaded.

What's the part i miss ?

Can someone help me ?

Thx in advance ;)

ps: Sorry if my english is full of mistake, i'm not a natural speaker.

Morgiver
  • 1
  • 2
  • the `keypair` that you're using inside the promise will not be available outside due to asynchronicity. [Checkout this very similar question](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) which talks in terms of callback rather that promises but fully applies in your case because `then(function(keypair){…})` is just a callback – laggingreflex Aug 29 '15 at 10:41
  • Please show the "outside "code where you are trying to use `keypair`. – Roamer-1888 Aug 29 '15 at 11:26
  • I give a link to pastebin http://pastebin.com/dkcUQVBP – Morgiver Aug 29 '15 at 12:11
  • In fact your node.js code should work even less. That whole `isGenerated` thing is the antipattern. Just return a promise instead! – Bergi Aug 29 '15 at 12:56
  • Well, i think i get it. I save all key in a cookie inside the promise, it's working fine. In NodeJS i will save the keys in a file or in a stream, sure it will be better than the isGenerated value. Promise are a big part of Javascript, not easy to understand the concept of asynchronicity. – Morgiver Aug 30 '15 at 09:43

0 Answers0