小编Art*_*ero的帖子

承诺,如何将变量传递给.then函数

您好,这是一个帮助我了解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)

javascript xmlhttprequest fetch promise

6
推荐指数
1
解决办法
5586
查看次数

标签 统计

fetch ×1

javascript ×1

promise ×1

xmlhttprequest ×1