-2

how can I make this call blocking (using, for example, the async/await)?

testMethod(message) {
    let signature;
    eccrypto.sign(this.privateKey, msg)
        .then(function (sig) {
            console.log("Signature in DER format:", sig);
            signature = sig;
        });
    return signature;
}

I'd like testMethod to return signature, right now return (of course) undefined! I've been playing with async/await with no success ...

any help?

Kasper
  • 685
  • 2
  • 11
  • 30
  • 2
    async/await are just ways to work with promises with nicer syntax. They don't stop async code being async. – Quentin Apr 02 '19 at 15:00
  • A minor thing, but you're using `message` in your function definition, but `msg` in your call to `eccrypto.sign`. Is that intentional? Which leads me to another point, you should always handle your errors, either using `.catch()` if using old-school promise syntax, or try/catch if using async/await – djheru Apr 02 '19 at 15:05

2 Answers2

1

Sure, you can do async/await. Like this

async testMethod(message) {
    let signature;
    signature = await eccrypto.sign(this.privateKey, msg)
        .then(function (sig) {
            console.log("Signature in DER format:", sig);
            return sig;
        });
    return signature;
}

But it won't be blocking. It would work similar to synchromous code but it's not the same. See for details.

Sergey Mell
  • 7,780
  • 1
  • 26
  • 50
0
async testMethod(msg) {
    try {
      const signature = await eccrypto.sign(this.privateKey, msg)
      console.log('Signature in DER format:', signature);
      return signature;
    } catch (e) {
      console.error('Error generating signature', e.message);
    }
}
djheru
  • 3,525
  • 2
  • 20
  • 20
  • This answer was flagged as low-quality because of its length and content. Could you consider adding some additional information explaining your answer. – Miroslav Glamuzina Apr 02 '19 at 18:57