您好,这是一个帮助我了解Promise如何.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 …Run Code Online (Sandbox Code Playgroud)