您好,这是一个问题,可以帮助我理解Promises .then
如何返回工作。 现在的问题是:如何将变量范围限定为第二个链接函数?承诺,如何将变量传递给。然后函数
这里是一个jsbin http://jsbin.com/xacuna/edit?js,output
我可以访问全局变量,并在范围变量第一遍以后,但不是之后。
let innerReturnFunction = (res, myName) => {
/* this works */
console.log(`hi from inner name: ${myName}`)
return res
}
let getInnerFuncVariable =() => {
var myName = 'arturo'
return fetch('https://httpbin.org/get')
.then(function (res) {
myName = 'Bob'
return innerReturnFunction(res, myName);
})
.then(function (res, myName) {
/* doesn't work, how can I access myName */
console.log(`in first then ${res.url}, ${myName}`)
});
}
getInnerFuncVariable().then(function(res, myName) {
/* how can I access myName */
console.log(`last called ${myName}`)
})
在随后的回调只接受一个参数 - 一个“对象”会很有用 –
请参阅[链接承诺时如何共享先前的结果](http://stackoverflow.com/questions/28714298/how-t邻链和股的在先结果与 - 承诺/ 28714863#28714863)。 – jfriend00