为什么即使在读取响应流后我的获取响应仍显示承诺?

我正在调用一个 API,它返回一个我需要访问的主体。通过 Postman 调用 API 按预期工作。JavaScript 代码如下:


async function checkIfCheater(nameToCheck) {

    let testJson = {

        "GameName" : nameToCheck

    }


    await fetch(apiGatewayCheckCheaterLocal, {

        method: 'POST',

        mode: 'cors',

        headers: {

            'Content-Type': 'application/json'

        },

        body: JSON.stringify(testJson)

    }).then(response=>response.json()).then(data=>{ return data; });

}

我尝试以多种方式访问响应。但是,尽管如此,返回值仍然是一个Promise {<pending>}我无法访问其主体的对象。

为什么我的对象仍然是一个承诺,尽管遵循了该链接上的答案,我该如何解决?


守着星空守着你
浏览 114回答 1
1回答

桃花长相依

应该更正如下,async function checkIfCheater(nameToCheck) {&nbsp; &nbsp; let testJson = {&nbsp; &nbsp; &nbsp; &nbsp; "GameName": nameToCheck&nbsp; &nbsp; };&nbsp; &nbsp; const res = await fetch(apiGatewayCheckCheaterLocal, {&nbsp; &nbsp; &nbsp; &nbsp; method: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Content-Type': 'application/json'&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; body: JSON.stringify(testJson)&nbsp; &nbsp; });&nbsp; &nbsp; const data = await res.json();&nbsp; &nbsp; return data;}checkIfCheater('name').then(data => console.log(data));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript