Javascript 如何在使用TableAsJSON调用函数后更改变量的值?

Javascript 如何在使用TableAsJSON调用函数后更改变量的值?,javascript,node.js,json,npm,Javascript,Node.js,Json,Npm,为什么调用函数后无法更改变量 这是我的代码: const tabletojson = require('tabletojson'); var email ; tabletojson.convertUrl( 'https://myurl , { stripHtmlFromCells: true }, function(tablesAsJson) { email = tablesAsJson[2][7][1]; var result2 = tablesA

为什么调用函数后无法更改变量

这是我的代码:

const tabletojson = require('tabletojson');

var email ;

tabletojson.convertUrl(

    'https://myurl
    ,
    { stripHtmlFromCells: true },
    function(tablesAsJson) {


  email = tablesAsJson[2][7][1];
var result2 = tablesAsJson;
        console.log(result2);
        var Firstname;
        var lastname;
        Firstname = tablesAsJson[0][1][1]
        lastname = tablesAsJson[0][0][1]

        console.log("Hello Sir: "+Firstname + "  " +lastname + ".  your email is : " + email)

        console.log(email)// this prints the correct answer
    }
  );
在尝试打印超出其功能范围的电子邮件时 返回带有 console.log(“电子邮件为”+电子邮件)


方法convertUrl是异步的,您不能在顶级代码中随意使用like wait。

如果您需要将此代码用作从模块导出的函数,则需要如下内容:

test-module.js:

test.js:


知道怎么做吗?使用您的方法,我得到了“等待仅在异步函数中有效”我认为您只能在回调或承诺中读取值(…)。然后(…)
'use strict';

const tabletojson = require('tabletojson');

async function getTableAsArray(url) {
  try {
    return await tabletojson.convertUrl(url);
  } catch (err) {
    console.error(err);
  }
}

module.exports = {
  getTableAsArray,
};
'use strict';

const testModule = require('./test-module.js');

(async function main() {
  try {
    const array = await testModule.getTableAsArray(
      'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes'
    );
    console.log(array[1][0]);
  } catch (err) {
    console.error(err);
  }
})();