我编写了一个 JavaScript 异步函数来使用我发现的检查拼写的 api:
async function checkWord(word) { var myHeaders = new Headers(); myHeaders.append("apikey", "My api key"); var requestOptions = { method: 'GET', redirect: 'follow', headers: myHeaders }; fetch(`https://api.apilayer.com/spell/spellchecker?q=${word}`, requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log('error', error)); }
要检查的单个单词作为字符串参数传递,例如:
let checkResult = checkWord('HOUSE'); console.log(checkResult);
我想重写这一行:
.then(result => console.log(result))
将结果作为 JSON 对象返回给调用者,但我不知道如何做到这一点。这是我尝试过的:
.then(result => () => {return result.json();})
有人可以提出一个修复方案来使这项工作按照我的意愿进行吗?我知道原始代码有效,因为原始 console.log 显示了有效结果。但我的函数调用后的 console.log 仅显示以下内容:
从函数中返回你的承诺并删除
async
,如下所示: