如何解决如何按照循环中传递给promise的数据的顺序返回值?
为了了解 fetch、promise 和其他 js 内容,我正在尝试编写一个小脚本,建议从给定的日语文本中学习(根据难度)单词。
它使用名为 Kuromojin 的日语解析器。
像 kuromojin 这样的解析器所做的是将短语标记为单词。
例如数据:“日本语が上手ですね!” → 标记词:[{surface_form: 日本语},{surface_form: が},{surface_form: 上手},{surface_form: です},{surface_form: ね},{surface_form: !}]
脚本首先从数据中分词,然后使用日语字典的 API (jisho.org) 获取每个分词的词对应的 JLPT 级别
app.js 文件:
import { tokenize,getTokenizer} from "kuromojin";
import { parseIntoJisho} from "./parseintojisho.js";
const text = "日本語が上手ですね!";
let tokenSet = new Set();
getTokenizer().then(tokenizer => {});
tokenize(text).then(results => { // the result is an array of objects
results.forEach((token) => {
tokenSet.add(token.surface_form);
})
console.log("======Begin to find JLPT level for below items======");
tokenSet.forEach((item) => {
console.log(item);
parseIntoJisho(item); // will throw a console log of whichever item's corresponding data is resolved,regardless of item order here,right?
});
})
parseintojisho.js 文件:
import fetch from 'node-fetch';
/* @param data is string */
export function parseIntoJisho(data) {
fetch(encodeURI(`https://jisho.org/api/v1/search/words?keyword=${data}`))
.then(res => res.json())
.then(jsondata => {
let JLPTvalueArray = jsondata.data[0].jlpt;
if (JLPTvalueArray.length) {
let jlptLevel = JLPTvalueArray.flatMap(str => str.match(/\d+/));
const max = Math.max(...jlptLevel);
if (max >= 3) {
console.log(data + " is of JLPT level N3 or above.")
} else console.log(data + " is of JLPT level N1 or N2.");
} else console.log(data + " has no JLPT value.")
})
.catch(function(err){
console.log("No data for " + data);
})
}
该脚本有效,但不是按顺序显示与每个标记词相对应的 JLPT 级别,而是随机显示。我猜哪个对应的数据先解析会出现在控制台日志中?
我发现Promise.All()
may solve my problem,但我找不到正确实现它的方法。
有没有办法按照传递到 parseIntoJisho(item);
的标记化项目的顺序放置获取的 JLPT 级别?
$ node app.js
======Begin to find JLPT level for below items======
日本語
が
上手
です
ね
!
です has no JLPT value. // should be "日本語 has no JLPT value." here instead
No data for ! // should be "が has no JLPT value." here instead
上手 is of JLPT level N3 or above.
日本語 has no JLPT value. // should be "です has no JLPT value." here instead
ね is of JLPT level N3 or above.
が has no JLPT value. // should be "No data for !" here instead
解决方法
使用 .map
而不是 forEach
,然后在所有异步请求完成后记录结果数组的每一项。
要将原始字符串传递给异步结果函数,请对 Promise.all
和原始数据使用 parseIntoJisho
。
Promise.all([...tokenSet].map(parseIntoJisho))
.then((results) => {
for (const [data,result] of results) {
if (!result) continue; // there was an error
if (result.length) {
const jlptLevel = result.flatMap(str => str.match(/\d+/));
const max = Math.max(...jlptLevel);
if (max >= 3) {
console.log(data + " is of JLPT level N3 or above.")
} else console.log(data + " is of JLPT level N1 or N2.");
} else console.log(data + " has no JLPT value.")
}
});
const parseIntoJisho = data => Promise.all([
data,fetch(encodeURI(`https://jisho.org/api/v1/search/words?keyword=${data}`))
.then(res => res.json())
.then(jsondata => jsondata.data[0].jlpt)
.catch(err => null)
]);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。