我正在处理一个组件,该组件读取CSV文件,并在文件的每一行发送请求。这一过程很好,但我试图显示反馈,因为线路被成功张贴。问题是,使用useState钩子,set函数在调用函数时传递,而不是在每个承诺都解决后传递。因此,我不能附加到成功的结果数组,数组一直被替换为最后一个成功的调用。API调用被取消一秒钟,以防止对服务器的过载。
import React, {useState} from "react";
import CSVReader from "react-csv-reader";
import {post} from "../api";
这两者之间有什么区别吗?
const promises = await Promise.all(items.map(e => somethingAsync(e)));
for (const res of promises) {
// do some calculations
}
这个呢?
for await (const res of items.map(e => somethingAsync(e))) {
// do some calculations
}
我知道在第一个片段中,所有的承诺都是同时发出的,但我不确定第二个。for循环是否等待第一次迭代来调用下一个承诺?还是
我在Javascript中有以下功能:
// fetches the username based on the user ID
const fetchUsername = async (userId) => {
const response = await axios({ ... });
return {
userId,
username: response.data.username
}
};
// fetches the online status based on the user ID
const fetchOnli
我有10个类似于以下功能的函数(数据库查询):
async loadEmails(){
this.emails = await (EmailEntity as any).findAll(this.where)
}
async loadUrls(){
this.urls = await (UrlEntity as any).findAll(this.where)
}
我一个接一个地给他们打电话。
我想加快执行速度。
for(const i in toLoad){
let name = toLoad[i];
name =