我曾经这样编码,最终发现自己陷入了回调 hell 。
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
不再。在每次返回时传递参数感觉很脏。
这个问题有什么优雅的解决方案吗?
最佳答案
Redis.getAsync("boo")
.then(res1 => {
return Redis.getAsync(res1).then(res2 => ({res1, res2}));
})
.then(({res1, res2}) => {
console.log(res1, res2);
});
关于javascript - 如何从旧的 then 调用中访问属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39246807/