javascript - 生成器函数中的多次提取
问题描述
当我试图学习如何使用生成器函数时,我遇到了使用它来运行多个提取的问题。
我正在尝试做的事情:
我正在尝试使用 Hackernews API 来获取新闻故事,然后使用响应中给出的 id 从同一个 API 获取评论。
输出:
{ value: Promise { }, done: false }
TypeError: Cannot read property 'kids' of undefined at getFirstComment (C:\Users\madsn\Documents\dev\generatorfunctions\fetchingInterTwice.js:13:29) at getFirstComment。 runThroughIt (C:\Users\madsn\Documents\dev\generatorfunctions\fetchingInterTwice.js:31:35) 处的 next () 在 processTicksAndRejections (internal/process/task_queues.js:93:5)
{ value: undefined, done: true }
{ 值:未定义,完成:真 }
代码:
const fetch = require("node-fetch")
function requestStuff(url) { ///simple request
return fetch(url).then(respJson => respJson.json()).catch(err => console.log(err));
}
function* getFirstComment() {
try {
//gets the story and the comments id(kids of type array)
var result1 = yield requestStuff("https://hacker-news.firebaseio.com/v0/item/8863.json?print=pretty")
var resID = result1.kids[0];
//gets the comment-text given the comment-id
var result2 = yield requestStuff("https://hacker-news.firebaseio.com/v0/item/" + resID + ".json?print=pretty")
var resText = result2.text
console.log(resText.text)
} catch (error) {
console.log(error)
}
}
let main = getFirstComment()
async function runThroughIt(generator) {
let stepOne = await generator.next()
console.log(stepOne)
let stepTwo = await generator.next()
console.log(stepTwo)
let stepThree = await generator.next()
console.log(stepThree)
}
runThroughIt(main)
如果您有修复或解释此文章的链接,请提前致谢!
解决方案
以下是您的代码的问题:
您的requestStuff
函数应该是async
并且返回值应该是网络请求的评估结果。目前,您只是返回 Promise 对象。
在将 yield
fetch表达式的结果分配给.result1
这是您可以解决的方法:
const fetch = require("node-fetch")
async function requestStuff(url) { ///simple request
const res = await fetch(url)
.then(respJson => respJson.json())
.catch(err => console.log(err));
return res;
}
async function* getFirstComment() {
try {
//gets the story and the comments id(kids of type array)
var result1 = await requestStuff("https://hacker-news.firebaseio.com/v0/item/8863.json?print=pretty");
yield result1;
var resID = result1.kids[0];
//gets the comment-text given the comment-id
var result2 = await requestStuff("https://hacker-news.firebaseio.com/v0/item/" + resID + ".json?print=pretty")
var resText = result2.text
console.log(resText.text)
yield resText;
} catch (error) {
console.log(error)
}
}
let main = getFirstComment()
async function runThroughIt(generator) {
let stepOne = await generator.next();
console.log("res from step 1! ", stepOne);
let stepTwo = await generator.next()
console.log("res from step 2: ", stepTwo);
//will return undefined because the function has finished evaluating!
let stepThree = await generator.next()
console.log(stepThree)
}
runThroughIt(main)
以上有效!现在特别注意在我的示例中调用yield
,await
和async
关键字的位置,并将其与您在代码中调用它的位置进行比较!
推荐阅读
- c++ - strstr() 有 constexpr 版本吗?
- tcl - TCL解释器默认启动目录
- css - 使用 css -webkit-overflow-scrolling 在科尔多瓦 ios 应用程序中面临内容呈现问题:触摸
- java - 如何比较 .SQL 文件语句(SQL 查询)是否在数据库中执行?
- angular - Angular:如何在没有事件的情况下获取 *ngFor DOM 元素后面的对象?
- javascript - 尽管 async-await,React 状态没有及时更新以进行渲染
- python - 检查一个列表是否以另一个列表的元素开始,如果找到,则将两个值一起返回
- rust - 如何在 rust 中使用 char_width?
- python-3.x - Python 3 + Selenium 3:在循环中使用 ActionChain 会引发 WebDriverException
- java - 在颤振应用程序中集成 GoogleTez 时出错