Javascript 向其他函数调用函数的返回值

Javascript 向其他函数调用函数的返回值,javascript,function,Javascript,Function,如何正确返回变量otherURL的值,并在同一文件的其他函数中调用/使用它 使用下面的代码 function getOtherURL() { var url = "https://url/data.json"; fetch(url) .then(res => res.json()) .then((data) => { console.log('Checkout this JSON! ', data); let othe

如何正确返回变量otherURL的值,并在同一文件的其他函数中调用/使用它

使用下面的代码

function getOtherURL() {
    var url = "https://url/data.json";

    fetch(url)
    .then(res => res.json())
    .then((data) => {
        console.log('Checkout this JSON! ', data);
        let otherURL;

        for (var i = 0; i < data.length; i++) {
        //some code
            otherURL = "http://url/from" + from + "&to=" + to;
        }
        console.log("otherURL" , otherURL);
    })
    .catch(err => { throw err });
}
如果可以修改getData函数,使其具有类似getDataotherURL的参数,则可以执行以下操作:

function getOtherURL() {
      const url = 'https://url/data.json';

      fetch(url)
    .then(res => res.json())
    .then((data) => {
      console.log('Checkout this JSON! ', data);
      let otherURL;

      for (let i = 0; i < data.length; i++) {
        // some code
        otherURL = `http://url/from${from}&to=${to}`;
      }
      console.log('otherURL', otherURL);
      return otherURL;
    })
    .then(otherURL => {
    // chain then() here
      getData(otherURL);
    })
    .catch((err) => {
      throw err;
    });
}

getOtherURL。然后。。。。。或者async/await,但在这两种情况下,您都需要返回fetch.im对您的getOtherURL感兴趣。那么….,您能完成吗?谢谢如何调用值?顺便说一句,catcherr=>{throw err};你可以删除它,它没有用..你能修改getData函数来有一个参数吗?你的答案对我有用。非常感谢你,卡里姆!:我认为我们缺少的一件事是如何在getOtherURL之外获取新URL。然后。。。?因为我需要在const promiseMSFT=fetchnewurl中使用它检查答案,所以我添加了最后一个这样做的promiseMSFT。再次感谢!谢谢你的帮助!这在控制台日志中给出了未定义的结果。
function getOtherURL() {
  var url = "https://url/data.json";
  return fetch(url)
  .then(res => res.json())
  .then((data) => {
    console.log('Checkout this JSON! ', data);
    let otherURL;
    for (var i = 0; i < data.length; i++) {
      //some code
      otherURL = "http://url/from" + from + "&to=" + to;
    }
    return otherUrl; //return value
  })
  .catch(err => { throw err });
}
export function getData() {
    //return promise to the caller
    return getOtherURL().then(otherUrl => {
      let newURL = otherURL;
      //then you can chain the other promise
      return fetch(newUrl);
    })
    .then(response => response.json())
    .then(data => {
       //more code
     })

}
function getOtherURL() {
      const url = 'https://url/data.json';

      fetch(url)
    .then(res => res.json())
    .then((data) => {
      console.log('Checkout this JSON! ', data);
      let otherURL;

      for (let i = 0; i < data.length; i++) {
        // some code
        otherURL = `http://url/from${from}&to=${to}`;
      }
      console.log('otherURL', otherURL);
      return otherURL;
    })
    .then(otherURL => {
    // chain then() here
      getData(otherURL);
    })
    .catch((err) => {
      throw err;
    });
}
export function getData(otherURL) {
  // need to read the value of otherURL and assign into new variable something like this
  let newURL = otherURL;
  console.log(newURL);
}