如何访问承诺中的功能

how to access a function in a promise

我在 React 应用程序中的代码

我有 我class:

class SampleModuleController{

    getSampleModuleSheet()
    {
       console.log("getSampleModuleSheet");
       return tableSheet;
    }
    retrivePageData(pageNumber,pageLength){
        
        console.log("retrivePageData calledd");
        let pageData= asyncAwaitService.findTablePageTestData(pageNumber,pageLength);
        return pageData;
    }
}

export let sampleModuleController = new SampleModuleController();

SampleModuleController class延迟加载,其getSampleModuleSheet方法可以成功使用。

在 jsx 中:

<DataTable getPageData={import('../controllers/sampleModuleContrller').then(({sampleModuleController}) => {return sampleModuleController.retrivePageData;})} />

在js文件中:

async newPageManager(){
    console.debug("this.props.getPageData------",this.props.getPageData);
    let pageData = await props.getPageData(1,34);
}

输出

那么我如何调用 promise 中的函数

在您的 jsx 中,promise returns 显然是一个方法委托。

因此,this.props.getPageData 在使用 await 解析时将生成一个方法委托,该方法委托本身将被调用。

我们将按如下方式修改您的代码段;

async newPageManager(){
    console.debug("this.props.getPageData------",this.props.getPageData);
    let getPageData = await props.getPageData; 
    let pageData = getPageData(1,34);
}

此外,由于 props.getPageData 正在返回一个承诺,因此 thenable

因此,您可以将该承诺的结果传递到 then 函数作用域中——类似于以下内容

async newPageManager(){
    console.debug("this.props.getPageData------",this.props.getPageData);
    let getFirstPagePromise = props.getPageData.then((fn) => fn.bind(this, 1, 32)); 
    let getFirstPage = await getFirstPagePromise;
    let pageData = getFirstPage();
}