javascript Axios重试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Axios重试相关的知识,希望对你有一定的参考价值。
import axios from 'axios';
async function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
};
export default async function retry(url,
options,
maximumRetry = 0,
attempt = 0,
delay = 0
) {
try {
await sleep(delay);
const { data } = await axios.request({
url,
...options
});
return data;
} catch (e) {
if (attempt >= maximumRetry) {
throw e;
}
return retry(url, options, maximumRetry, attempt + 1, (delay || 1000) * 1.3);
}
};
async getChatInfo(appId, previousUrl, metaToken) {
const res = await retry(
`${this._serviceURL}/router`,
{
headers: {
PreviousUrl: previousUrl,
MetaTagToken: metaToken
},
params: {
appId
},
withCredentials: true
}, 3
);
return res;
}
{
"plugins": ["transform-runtime"],
"presets": ["es2015", "stage-0"]
}
{
"name": "ps-chat-webclient",
"version": "1.7.1-2",
"description": "Cliente JS responsável por carregar Chats nas aplicações do PagSeguro",
"main": "dist/main.js",
"files": [
"/dist"
],
"dependencies": {
"axios": "^0.19.0",
"babel-polyfill": "^6.26.0",
"babel-preset-es2016": "^6.24.1",
"babel-runtime": "^6.26.0",
"promise-polyfill": "^8.1.0"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.26.0",
"clean-webpack-plugin": "^1.0.0",
"copy-webpack-plugin": "^4.6.0",
"css-loader": "^2.1.0",
"eslint": "^5.8.0",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.5.0",
"node-sass": "^4.11.0",
"nyc": "^13.1.0",
"sass-loader": "^7.1.0",
"sinon": "^7.1.1",
"uglify-js": "^3.4.9",
"uglifyjs-webpack-plugin": "^2.0.1",
"webpack": "^4.25.1",
"webpack-cli": "^3.3.2",
"webpack-dev-server": "^3.4.1"
},
"scripts": {
"start": "json-server --watch chat.api.mock.json --routes chat.api.mock.custom-routes.json & webpack-dev-server --hot --inline --color --progress --config webpack.dev.config.js --open",
"build:prod": "webpack -p --mode=production --config webpack.prod.config.js --display verbose",
"build:qa": "webpack -p --mode=production --config webpack.qa.config.js --display verbose",
"test": "nyc mocha"
},
"nyc": {
"include": [
"src/**/*.js",
"src/*.js"
],
"exclude": [
"src/vendors/*.js",
"src/index.js"
],
"reporter": [
"text-summary",
"html"
],
"sourceMap": true,
"instrument": true
},
"author": "\"Munich <l-scrum-munich@uolinc.com>",
"license": "Apache-2.0",
"publishConfig": {
"registry": "http://nexus.pagseguro.intranet/repository/npm-pagseguro/"
}
}
const path = require('path');
const chokidar = require('chokidar');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { DefinePlugin } = require('webpack');
const devConfig = require('./build.config.json').develop;
Object.keys(devConfig).forEach(key => {
devConfig[key] = JSON.stringify(devConfig[key]);
});
module.exports = {
devServer: {
before(app, server) {
chokidar.watch([
'./src/**/*.html'
]).on('all', function() {
server.sockWrite(server.sockets, 'content-changed');
});
},
contentBase: path.resolve(__dirname, './dist'),
watchOptions: {
poll: true
},
watchContentBase: true,
compress: true,
port: 8080,
publicPath: '/',
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
'Access-Control-Allow-Credentials': true
}
},
devtool: 'source-maps',
mode: 'development',
entry: ['babel-polyfill', 'promise-polyfill/src/polyfill', './src/index.js', './src/styles/index.scss',
'webpack-dev-server/client?http://localhost:8080',
'webpack/hot/only-dev-server'
],
output: {
path: path.resolve(__dirname, './dist'),
filename: 'ps-chat-client.js',
library: 'bundle_[name]',
publicPath: './'
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}, {
test: /\.(scss|sass)$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
}, {
test: /\.html$/,
include: path.resolve(__dirname, './src/'),
use: {
loader: 'html-loader',
options: {
minimize: true,
interpolate: true,
collapseWhitespace: true,
conservativeCollapse: false
}
}
}]
},
optimization: {
minimize: true
},
plugins: [
new CleanWebpackPlugin(['dist']),
new DefinePlugin(devConfig),
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
compress: {
drop_console: false
},
comments: false,
ecma: 6,
mangle: true
},
sourceMap: true
}),
new MiniCssExtractPlugin({
filename: 'main.css'
}),
new HtmlWebpackPlugin({
inject: false,
title: 'Chat Webclient',
dataChatApp: 'ps-faq',
template: './src/index.ejs'
}),
new CopyWebpackPlugin([{
from: './src/vendors',
to: './vendors/',
force: true,
toType: 'dir'
}])
]
};
以上是关于javascript Axios重试的主要内容,如果未能解决你的问题,请参考以下文章