从异步函数返回的承诺中获得价值 [英] Getting value from returned promise from async function
问题描述
我习惯所提出的异步/的await语法和有一些非直观行为。里面的异步功能,我可以CONSOLE.LOG正确的字符串。然而,当我试图返回该字符串,相反,它会返回一个承诺。
检查此项:? ES7异步/等待隐含返回承诺,这是pretty清楚,任何异步函数()将返回一个承诺,而不是一个值。没关系。但你如何可以访问的价值?如果唯一的答案是回调,这很好 - 但我希望有可能是一个更优雅。
// SRC
// ==========================================要求(巴别塔,填充工具);
VAR bcrypt =要求('bcrypt');VAR saltAndHash =功能(密码){
无功散;
返回新希望(函数(解析,拒绝){
bcrypt.genSalt(10,函数(ERR,盐){
bcrypt.hash(口令,盐,功能(ERR,哈希){
解决(散);
});
});
})
}VAR makeHash =异步功能(密码){
VAR哈希=等待saltAndHash(密码);
的console.log(内makeHash,哈希值);
返回(散);
}//从测试套件
// ==========================================描述(Bcrypt哈希',函数(){ 它('应该生成一个散列',函数(){
VAR哈希= makeHash('密码');
的console.log(内部测试,哈希值);
should.exist(散);
})})//输出到控制台:
// ========================================== 内部测试:无极{
_D:
{号码:[通知]
C: [],
答:不确定,
S:0,
D:假的,
五:未定义,
H:假的,
N:假}} 里面MakeHash $ 2A $ 10 $ oUVFL1geSONpzdTCoW.25uaI / LCnFqeOTqshAaAxSHV5i0ubcHfV6 //等
// ==========================================
// .babelrc
{$ P $的pset:[ES2015,舞台-0,反应]}
是的,你只能使用一个回调访问:
makeHash('密码'),然后(哈希=>的console.log(散));
或课程,你可以只是使等待它的另一个功能异步:
它('应该生成一个散列,异步函数(){
VAR哈希=等待makeHash('密码');
的console.log(内部测试,哈希值);
should.exist(散);
})
有没有办法访问同步的承诺的结果。
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: ES7 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"] }
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.
这篇关于从异步函数返回的承诺中获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!