Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value from returned promise from async function

I'm getting used to the proposed async/await syntax and there's some unintuitive behavior. Inside the "async" function, I can console.log the correct string. However, when I try to return that string, instead, it returns a promise.

Checking this entry: async/await implicitly returns promise? , it's pretty clear that any "async function()" will return a promise, NOT a value. That's fine. But how do you get access to the value? If the only answer is "a callback", that's fine - but I was hoping there might be something more elegant.

// src 
// ==========================================

require("babel-polyfill");
var bcrypt = require('bcrypt');

var saltAndHash = function(password){
  var hash;
  return new Promise(function(resolve, reject){
    bcrypt.genSalt(10, function(err, salt) {
      bcrypt.hash(password, salt, function(err, hash) {
          resolve(hash);
      });
    });
  })
}

var makeHash = async function(password){
  var hash = await saltAndHash(password);
  console.log("inside makeHash", hash); 
  return(hash); 
}

// from test suite
// ==========================================

describe('Bcrypt Hashing', function(){

  it('should generate a hash', function(){
    var hash = makeHash('password');
    console.log("inside test: ", hash); 
    should.exist(hash);
  })

})

// output to console:
// ==========================================

  inside test:  Promise {
  _d: 
   { p: [Circular],
     c: [],
     a: undefined,
     s: 0,
     d: false,
     v: undefined,
     h: false,
     n: false } }

  inside MakeHash $2a$10$oUVFL1geSONpzdTCoW.25uaI/LCnFqeOTqshAaAxSHV5i0ubcHfV6

  // etc 
  // ==========================================
  // .babelrc
    {  "presets": ["es2015", "stage-0", "react"] }
like image 904
Brian Boyko Avatar asked Feb 11 '16 11:02

Brian Boyko


People also ask

Can an async function return a promise?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

How do you find the return value of a promise object?

Define an async function. Use the await operator to await the promise. Assign the result of using the await operator to a variable.

How do you find the value of promises?

It is the fetch() function that returns a value, which is a Promise instance. It is the Promise instance on which you call the then() method, passing in a callback function, which will be eventually be fired when the async code finishes (and internally, calls resolve() ).

How do I return after promise?

Promise resolve() method: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.


1 Answers

Yes, you only can access it using a callback:

makeHash('password').then(hash => console.log(hash));

Or of course, you can just make another async function that waits for it:

it('should generate a hash', async function(){
  var hash = await makeHash('password');
  console.log("inside test: ", hash); 
  should.exist(hash);
})

There is no way to access the result of a promise synchronously.

like image 148
Bergi Avatar answered Nov 08 '22 04:11

Bergi