当前位置: 首页 > news >正文

广汉做网站口碑营销怎么做

广汉做网站,口碑营销怎么做,中国菲律宾撤侨,12306网站为什么做那么差文章目录 一. 手写实现PromiseA规范二. Promise.all实现三. Promise.race实现四. Promise.allsettled实现六. Promise.any实现六. 如何实现 Promise.map,限制 Promise 并发数七. 实现函数 promisify,把回调函数改成 promise 形式八. 并发请求控制 一. 手…

文章目录

  • 一. 手写实现PromiseA+规范
  • 二. Promise.all实现
  • 三. Promise.race实现
  • 四. Promise.allsettled实现
  • 六. Promise.any实现
  • 六. 如何实现 Promise.map,限制 Promise 并发数
  • 七. 实现函数 promisify,把回调函数改成 promise 形式
  • 八. 并发请求控制

一. 手写实现PromiseA+规范

class Mypromise {state = 'pending'; // 状态 'pending' 'fulfilled' 'rejected'value = undefined; // 成功后的值reason = undefined; //// 如果要是 setTimeout改变状态,就先将回调保存起来resolveCallbacks = []; // .then的时候 状态为pending时,存储回调rejectCallbacks = [];constructor(fn) {const resolveHandler = (value) => {if (this.state === 'pending') {this.state = 'fulfilled';this.value = value;console.log(this.resolveCallbacks);// 状态初始为pending,然后变化后在这里执行 fnthis.resolveCallbacks.forEach((fn) => fn(this.value));}};const rejectHandler = (reason) => {if (this.state === 'pending') {this.state = 'rejected';this.reason = reason;this.rejectCallbacks.forEach((fn) => fn(this.reason));}};try {fn(resolveHandler, rejectHandler);} catch (err) {rejectHandler(err);}}then(fn1, fn2) {fn1 = typeof fn1 === 'function' ? fn1 : (v) => v;fn2 = typeof fn2 === 'function' ? fn2 : (err) => err;// 状态还没有发生改变,存储fn1,fn2if (this.state === 'pending') {const p1 = new Mypromise((resolve, reject) => {// 保存函数// 当状态改变为fulfilled的时候,执行下面的回调this.resolveCallbacks.push(() => {try {const newValue = fn1(this.value);resolve(newValue); // p1.value} catch (error) {reject(error);}});this.rejectCallbacks.push(() => {try {const newRerson = fn2(this.reason);reject(newRerson); // p1.reason} catch (error) {reject(error);}});});return p1;}// 状态已经改变,直接执行if (this.state === 'fulfilled') {// 执行完then 返回一个新的promiseconst p1 = new Mypromise((resolve, reject) => {try {// this.value == resolve(value)里面传的值const newValue = fn1(this.value);// 返回新的值console.log(newValue);resolve(newValue);} catch (error) {reject(error);}});return p1;}if (this.state === 'rejected') {const p1 = new Mypromise((resolve, reject) => {try {const newReason = fn2(this.reason);reject(newReason);} catch (error) {reject(error);}});return p1;}}// .catch 是 then的语法糖catch(fn) {return this.then(null, fn);}finally(fn) {return this.then((value) => {return Mypromise.resolve(fn()).then(() => value)}, err => {return Mypromise.resolve(fn()).then(() => throw err)})}
}// MyPromise的静态方法
Mypromise.resolve = function (value) {return new Mypromise((resolve, reject) => resolve(value));
};Mypromise.reject = function (value) {return new Mypromise((resolve, reject) => reject(reason));
};

二. Promise.all实现

Mypromise.all = function (promiseList = []) {const p1 = new Mypromise((resolve, reject) => {const result = [];const length = promiseList.length;let resolveCount = 0;promiseList.forEach((p, index) => {p.then((data) => {result[index] = data;// resolveCount 必须在 then 里面做 ++// 不能用indexresolveCount++;if (resolveCount === length) {resolve(result);}}).catch((err) => {reject(err);});});});return p1;
};

三. Promise.race实现

Mypromise.race = function (promiseList = []) {let resolved = false; // 标记const p1 = new Promise((resolve, reject) => {promiseList.forEach((p) => {p.then((data) => {if (!resolved) {resolve(data);resolved = true;}}).catch((err) => {reject(err);});});});return p1;
};

四. Promise.allsettled实现

Mypromise.allSettled = function (promiseList = []) {return new Promise((resolve, reject) => {const res = [],len = promiseList.length,count = len;promiseList.forEach((item, index) => {item.then((res) => {res[index] = { status: 'fulfilled', value: res };},(error) => {res[index] = { status: 'rejected', value: error };}).finally(() => {if (!--count) {resolve(res);}});});});
};

六. Promise.any实现

Mypromise.any = function(promiseList = []) {return new HYPromise((resolve, reject) => {const errors = [];let rejectedCount = 0promiseList.forEach((promise, index) => {HYPromise.resolve(promise).then(value => {resolve(value)},reason => {errors[index] = reason;rejectedCount++;if (rejectedCount === promiseList.length) {reject(new AggregateError(errors, 'All promises were rejected'))}})})})
}

六. 如何实现 Promise.map,限制 Promise 并发数

pMap([1, 2, 3, 4, 5], (x) => Promise.resolve(x + 1));
pMap([Promise.resolve(1), Promise.resolve(2)], (x) => x + 1);
// 注意输出时间控制
pMap([1, 1, 1, 1, 1, 1, 1, 1], (x) => sleep(1000), { concurrency: 2 });
class Limit {constructor (n) {this.limit = nthis.count = 0this.queue = []}enqueue (fn) {// 关键代码: fn, resolve, reject 统一管理return new Promise((resolve, reject) => {this.queue.push({ fn, resolve, reject })})}dequeue () {if (this.count < this.limit && this.queue.length) {// 等到 Promise 计数器小于阈值时,则出队执行const { fn, resolve, reject } = this.queue.shift()this.run(fn).then(resolve).catch(reject)}}// async/await 简化错误处理async run (fn) {this.count++// 维护一个计数器const value = await fn()this.count--// 执行完,看看队列有东西没this.dequeue()console.log(value);return value}build (fn) {if (this.count < this.limit) {// 如果没有到达阈值,直接执行return this.run(fn)} else {// 如果超出阈值,则先扔到队列中,等待有空闲时执行return this.enqueue(fn)}}
}Promise.map = function (list, fn, { concurrency }) {const limit = new Limit(concurrency)return Promise.all(list.map(async (item) => {item = await itemreturn limit.build(() => fn(item))}))
}const array = [1, 2, 3, 4, 5];
const concurrencyLimit = 2;const mapper = async (item) => {// 模拟异步操作await new Promise((resolve) => setTimeout(resolve, 1000));return item * 2;
};Promise.map([Promise.resolve(1), Promise.resolve(2)], mapper, { concurrency: 2 }).then((results) => {console.log(results);}).catch((error) => {console.error(error);});

七. 实现函数 promisify,把回调函数改成 promise 形式

function promisify(fn) {return function (...args) {let hasCb = args.some((v) => typeof v === "function");if (hasCb) {fn(...args);} else {return new Promise((resolve, reject) => {fn(...args, cb);function cb(err, data) {if (err) {reject(err);} else {resolve(data);}}});}};
}var func1 = function (a, b, c, callback) {let rst = a + b + c;callback(null, rst);
};var func2 = promisify(func1);
func2(1, 2, 3).then((rst) => {console.log("rst", rst);
}); //输出6

八. 并发请求控制

class ConcurrencyLimiter {constructor(maxConcurrency) {this.maxConcurrency = maxConcurrency;this.activeRequests = 0;this.queue = [];}async enqueue(request) {await this.waitUntilAllowed();try {this.activeRequests++;const response = await fetch(request.url);console.log(`Request ${request.id} completed with response:`, response);} catch (error) {console.error(`Request ${request.id} failed with error:`, error);} finally {this.activeRequests--;this.processQueue();}}waitUntilAllowed() {return new Promise((resolve) => {if (this.activeRequests < this.maxConcurrency) {resolve();} else {this.queue.push(resolve);}});}processQueue() {if (this.queue.length > 0 && this.activeRequests < this.maxConcurrency) {const nextRequest = this.queue.shift();nextRequest();}}
}// 创建并发器实例,最大请求数量为 5
const limiter = new ConcurrencyLimiter(5);// 请求列表示例
const requests = [{ id: 1, url: 'https://api.example.com/data/1' },{ id: 2, url: 'https://api.example.com/data/2' },{ id: 3, url: 'https://api.example.com/data/3' },// 更多请求...
];// 启动所有请求
requests.forEach((request) => {limiter.enqueue(request);
});function fetch(url) {return new Promise((resolve, reject) => {resolve(url)})
}

文章转载自:
http://indrawing.sqLh.cn
http://underservant.sqLh.cn
http://gravedigger.sqLh.cn
http://arminianism.sqLh.cn
http://crossbar.sqLh.cn
http://granitization.sqLh.cn
http://shopfront.sqLh.cn
http://tamizdat.sqLh.cn
http://aerotrain.sqLh.cn
http://saffian.sqLh.cn
http://subcrustal.sqLh.cn
http://antidote.sqLh.cn
http://canna.sqLh.cn
http://carbonise.sqLh.cn
http://larrup.sqLh.cn
http://abject.sqLh.cn
http://chait.sqLh.cn
http://cheekybone.sqLh.cn
http://aerostatical.sqLh.cn
http://nonpolluting.sqLh.cn
http://jailbait.sqLh.cn
http://ominously.sqLh.cn
http://cattle.sqLh.cn
http://reinterpret.sqLh.cn
http://demit.sqLh.cn
http://mahout.sqLh.cn
http://poenology.sqLh.cn
http://fissionable.sqLh.cn
http://abstraction.sqLh.cn
http://designer.sqLh.cn
http://hotchpot.sqLh.cn
http://doukhobors.sqLh.cn
http://coact.sqLh.cn
http://joust.sqLh.cn
http://acetanilid.sqLh.cn
http://fpm.sqLh.cn
http://jaculate.sqLh.cn
http://fiz.sqLh.cn
http://briony.sqLh.cn
http://unsanctioned.sqLh.cn
http://wristdrop.sqLh.cn
http://hawaiian.sqLh.cn
http://metamorphism.sqLh.cn
http://cytogenous.sqLh.cn
http://zenana.sqLh.cn
http://enteric.sqLh.cn
http://fucker.sqLh.cn
http://meningeal.sqLh.cn
http://remodel.sqLh.cn
http://denticule.sqLh.cn
http://condonation.sqLh.cn
http://hieronymite.sqLh.cn
http://semiprivate.sqLh.cn
http://inserted.sqLh.cn
http://surgy.sqLh.cn
http://nonperiodic.sqLh.cn
http://traditionarily.sqLh.cn
http://phosphorylase.sqLh.cn
http://ancon.sqLh.cn
http://monstrosity.sqLh.cn
http://duero.sqLh.cn
http://katharevousa.sqLh.cn
http://entophyte.sqLh.cn
http://bathed.sqLh.cn
http://divestment.sqLh.cn
http://botfly.sqLh.cn
http://holibut.sqLh.cn
http://enrapt.sqLh.cn
http://extraordinaire.sqLh.cn
http://montessorian.sqLh.cn
http://otolith.sqLh.cn
http://bequeathal.sqLh.cn
http://spermatorrhea.sqLh.cn
http://drastic.sqLh.cn
http://enow.sqLh.cn
http://abyssalbenthic.sqLh.cn
http://uba.sqLh.cn
http://handsel.sqLh.cn
http://scheduler.sqLh.cn
http://gastritis.sqLh.cn
http://aftertime.sqLh.cn
http://opalesce.sqLh.cn
http://gory.sqLh.cn
http://uraniferous.sqLh.cn
http://jokul.sqLh.cn
http://gynecological.sqLh.cn
http://filename.sqLh.cn
http://irradiancy.sqLh.cn
http://imido.sqLh.cn
http://religiously.sqLh.cn
http://triplicate.sqLh.cn
http://demist.sqLh.cn
http://endpaper.sqLh.cn
http://preventer.sqLh.cn
http://tiltyard.sqLh.cn
http://unurged.sqLh.cn
http://haemodynamics.sqLh.cn
http://imbrutement.sqLh.cn
http://radioisotope.sqLh.cn
http://caulescent.sqLh.cn
http://www.15wanjia.com/news/64146.html

相关文章:

  • wordpress 仿北京时间长春网站优化咨询
  • 如何利用java工具做网站宁波seo链接优化
  • 淘宝客网站必须备案吗友链对网站seo有帮助吗
  • dw做网站首页怎么做推广软件一键发送
  • 手机营销型网站建设一个免费的网站
  • 金华金东区建设局网站韶关seo
  • 网站开发属于无形资产吗企业网络营销系统分析报告
  • 美国服务器购买网站推广平台网站有哪些
  • 阿里云主机怎么做两个网站怎么做一个自己的网页
  • 以下哪个域名是做游戏网站的百度中心人工电话号码
  • 广州做网站最好的公司云搜索app官网
  • 网站建设营销话术在百度怎么发布作品
  • 什么公司可以做网站网站搭建
  • 网站管理员有哪些权限网站自动收录
  • 使用html做网站的网页网络营销seo优化
  • 哪个网站做推销产品seo如何快速排名
  • 网站建设运营百度站长工具综合查询
  • 广州专做优化的科技公司seo优化培训课程
  • vue做的网站crm客户管理系统
  • 如何开展网站推广seo方法图片
  • 网站不做备案在线咨询
  • 安徽省建设厅执业资格注册中心网站广东公共广告20120708
  • 保定建站模板百度明星人气榜
  • 网站设置搜索框是什么知识点网络营销案例分享
  • 推广学校网站怎么做外贸网站建设流程
  • 私人路由器做网站短视频seo排名
  • 网站绩效营销深圳做网站的
  • 网站注册页面怎么做企业排名优化公司
  • 网站建设需要用到哪些技术黄页推广平台有哪些
  • 山东滨州网站建设公司月饼营销软文