Skip to content

Instantly share code, notes, and snippets.

View zhongyangxun's full-sized avatar
🦄
Slow down

zhongyangxun zhongyangxun

🦄
Slow down
View GitHub Profile
@zhongyangxun
zhongyangxun / MyPromise.js
Last active March 13, 2021 13:07
手写 Promise.
// Promise A+: https://promisesaplus.com/
function MyPromise(fn) {
this.status = 'pending';
this.value = null;
this.callbacks = [];
this.resolve = (newVal) => {
const fn = () => {
if (this.status !== 'pending') {
@zhongyangxun
zhongyangxun / myRedux.js
Created March 24, 2021 14:39
手写简易 Redux
function createStore(state, reducer) {
const listeners = [];
const getState = () => state;
const dispatch = (action) => {
state = reducer(state, action);
listeners.forEach((item) => item());
}
@zhongyangxun
zhongyangxun / concurrent-request.html
Created April 3, 2021 15:39
限制并发请求数量的方法。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Concurrent request</title>
</head>
<body>
<button id="fetch">fetch</button>
@zhongyangxun
zhongyangxun / shuffle.js
Created April 14, 2021 16:12
数组乱序。
function shuffle(arr) {
if (!arr || arr.length === 0) {
return [];
}
if (arr.length === 1) {
return [...arr];
}
const map = new Map();