小编dan*_*sto的帖子

有什么方法可以发出递归获取请求吗?

我希望我的提取请求有某种重试系统,如果它基于响应的 HTTP 代码(例如:不是 200)以某种方式失败。它看起来像这样:

fetch('someURLWithAJSONfile/file.json')
        .then(function (res) {
            console.log(res.status);
            if (res.status !== 200) {
                console.log("There was an error processing your fetch request. We are trying again.");

// Recursive call to same fetch request until succeeds

            } else {
                return res.json();
            }
        }).then(function (json) {
        data = json;
    }).catch(function (err) {
        console.log(`There was a problem with the fetch operation: ${err.message}`);
    });
Run Code Online (Sandbox Code Playgroud)

有没有办法将获取请求放入自定义 Promise 中,并使其在检查其 http 响应状态后调用自身?

javascript fetch node.js express ecmascript-6

2
推荐指数
1
解决办法
1万
查看次数

如何在 React 中引用 pdf 文件

我有一个 Reactjs SPA,其文件结构如下所示:

\n\n

在此输入图像描述

\n\n

在该应用程序中有一个简单的组件(Resume.js),我希望用户能够在浏览器中查看/下载pdf文件(resume_english.pdf)。然而,我无法在本地或在我计划托管 SPA 的 GitHub 页面中执行此操作。它总是将我重定向到主页。

\n\n

Resume.js看起来像这样:

\n\n
import React, {Component} from \'react\';\n\nclass Resume extends Component {\n    constructor(props) {\n        super(props);\n\n        this.state = {};\n    }\n\n    render() {\n        return (\n            <div className={\'resume-container\'}>\n                <div id={\'resume-pdf\'}>You can view, download and print my r\xc3\xa9sum\xc3\xa9 in pdf format in <a\n                    href={\'./public/resume_english.pdf\'}>English</a> and <a href={\'#\'}>Spanish</a></div>\n            </div>\n        );\n    }\n}\n\nexport default Resume;\n
Run Code Online (Sandbox Code Playgroud)\n\n

关于如何解决它可能有什么问题有什么想法吗?我已经尝试过像这样导入,但没有成功。

\n

javascript filepath single-page-application reactjs

2
推荐指数
1
解决办法
6195
查看次数

在Common Lisp中交换列表元素

我需要交换给定两个位置(i,j)的列表元素来实现TSP的2-opt启发式,并发现这个问题建议使用rotatef.

但是,当我尝试使用它时,我无法理解它的行为.

这段代码令我头疼:

(setq loop '(A B C D E F))
> (A B C D E F)

;; Original copy saved in loop
(setq new_tour loop)

(format t "ORIGINAL LOOP: " loop)
> LOOP: (A B C D E F)

(format t "NEW_TOUR: " new_tour)
> NEW_TOUR: (A B C D E F)

(rotatef (nth 0 new_tour) (nth 1 new_tour))
(format t "NEW_TOUR IS NOW: ~a~%" new_tour)
> NEW_TOUR IS NOW: (B A C D …
Run Code Online (Sandbox Code Playgroud)

lisp common-lisp

1
推荐指数
1
解决办法
258
查看次数

性能读取二进制文件

我有一个程序从一个非常大的二进制文件(48 MB)读取,然后将数据传递给名为像素的自定义结构矩阵:

struct pixel {
    int r;
    int g;
    int b;
};
Run Code Online (Sandbox Code Playgroud)

打开文件:

ifstream myFile(inputPath, ios::binary);
pixel **matrixPixel;
Run Code Online (Sandbox Code Playgroud)

以这种方式读取文件:

int position = 0;

for (int i = 0; i < HEIGHT; ++i) {
        for (int j = 0; j < WIDTH; ++j) {
            if (!myFile.eof()) {
                myFile.seekg(position, ios::beg);
                myFile.read((char *) &matrixPixel[i][j].r, 1); // red byte
                myFile.seekg(position + HEIGHT * WIDTH, ios::beg);
                myFile.read((char *) &matrixPixel[i][j].g, 1); // green byte
                myFile.seekg(position + HEIGHT * WIDTH * 2, ios::beg);
                myFile.read((char *) &matrixPixel[i][j].b, 1); …
Run Code Online (Sandbox Code Playgroud)

c++ io optimization performance file

0
推荐指数
1
解决办法
604
查看次数