0
const _sodium = require('libsodium-wrappers');
(async (model = this._modelUser) => { //Had to pass this._modelUser otherwise it'd be undefined
    await _sodium.ready;
    const sodium = _sodium;

    let password = sodium.crypto_pwhash_str(Utils.urldecode(data.password), 7, 677445);
    model.password = password;
    console.log('In:' + model.password); //Good value
})();

console.log('Out:' + this._modelUser.password); //Undefined

So in this case, this._modelUser.password is undefined outside function. I'd like to wait for this._modelUser to get the right password value before continuing.

Does anyone have an idea about how to fix this ? Thanks for your help

Different than How do I return the response from an asynchronous call? because of the syntax (async => ()) and because of usage of a specific module: https://www.npmjs.com/package/libsodium

Dushy
  • 37
  • 7
  • 2
    Possible duplicate of [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) – CertainPerformance Jan 22 '19 at 10:19

2 Answers2

0

so the purpose of async/await is to be none blocking and returns a Promise object that you can use to act on what is returned.

It is difficult to know exactly what you plan to do with your model after assigning your password, but I would recommend you assign your password to your model outside of your current function with something like this:

const _sodium = require('libsodium-wrappers');

const generatePassword = async () => {
    await _sodium.ready;
    const sodium = _sodium;

    // Not sure where you have got data from, could be passed in locally.
    return sodium.crypto_pwhash_str(Utils.urldecode(data.password), 7, 677445);
}

generatePassword()
    .then((password) => {
        this._modelUser.password = password;
    })
    .catch((error) => console.log('error: ', error))

You can of course split this up even more to be more functional.

  • Hi Christopher, thanks for you help I tried your code, but I keep falling into the catch. I added some console.log inside generatePassword, data.password is well defined, and sodium.crypto_pwhash_str send a good hash. Is there a way to check why it got catched ? Thanks – Dushy Jan 22 '19 at 11:09
  • Hi Dushy, I see you have gone with the other answer, but yeah the error comes through as a parameter in the catch, I will update the code to reflect this – Christopher Slater Jan 22 '19 at 11:48
0

Promises help in achieving this .



const _sodium = require('libsodium-wrappers');
(async (model = this._modelUser) => { d
    await _sodium.ready;
    const sodium = _sodium;

    let password = sodium.crypto_pwhash_str(Utils.urldecode(data.password), 7, 677445);
    model.password = password;

    return new Promise((resolve, reject) => {
    if(model.password) resolve(model.password); 
    else reject(model)
   })
})()
.then((a) =>{
//handling code
})
.catch((a) => {
//handling code
})
Footer
  • 129
  • 4
  • I tried your method, but instead resolve(model.password) I put resolve(model) So in then(), I did: this._modelUser = a; Otherwise, this._modelUser became null. Thanks again! – Dushy Jan 22 '19 at 11:37