我曾经像这样编写代码,最终发现自己陷入了回调地狱。
Redis.get("boo", (res1) => {
Redis.get(res1, (res2) => {
console.log(res1);
console.log(res2);
});
});然而,当我这样做的时候:
Redis.getAsync("boo)
.then(res1 => {
return Redis.getAsync(res1);
})
.then(res2 => {
console.log(res1) // undefined
});我无法再访问res1。在每个返回值上传递参数感觉很脏。
这个问题有什么优雅的解决方案吗?
发布于 2016-08-31 18:22:18
Redis.getAsync("boo")
.then(res1 => {
return Redis.getAsync(res1).then(res2 => ({res1, res2}));
})
.then(({res1, res2}) => {
console.log(res1, res2);
});发布于 2016-08-31 18:38:48
这是,不是官方支持的,但是...
如果你喜欢冒险,乐于使用aync,那么使用await/ babel怎么样?
async function foo() {
const res1 = await Redis.getAsync("boo")
const res2 = await Redis.getAsync(res1)
}https://stackoverflow.com/questions/39246807
复制相似问题