比如,
Q "Hello world"
.then (str)->
"hello 2"
.then (str2)->
# str 怎么获取?
备选方案 1:闭包法
Q "hello world"
.then (str)->
Q "hello 2"
.then (str2)->
console.log str, str2
缺点:不好看,陷入了 callback hell。
备选方案 2:外部变量法
str = null
Q "hello world"
.then (s)->
s = str
"hello 2"
.then (str2)->
console.log str, str2
缺点:用跨越作用域的变量,显得不太干净
备选方案 3:返回数组法
Q "hello world"
.then (str)->
[str, "hello 2"]
.then (s)->
console.log s[0], s[1]
缺点:返回值意义不明朗;要向下传递多层的时候麻烦。
所以大家遇到这种问题的时候怎么办?