这篇文章主要介绍“如何用promsie实现观察者模式”,在日常操作中,相信很多人在如何用promsie实现观察者模式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何用promsie实现观察者模式”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
下面我们用一个示例来演示一下什么是观察者模式,有这样一个场景,在一个院子里,有一个小偷,和若干条狗,小偷只要一行动,狗就会叫,这个场景如果用图来展示的话如图:
// 第一版
class Thief {
constructor(){
}
// thief的方法,调用dog的方法;
action(){
dog1.call()
dog2.call()
dog3.call()
}
}
class Dog {
call(){
console.log("狗叫")
}
}
let dog1 = new Dog()
let dog2 = new Dog()
let dog3 = new Dog()
let thief = new Thief();
thief.action()
// 第一版-新增dog4
class Thief {
constructor() {
}
// thief的方法,调用dog的方法;
action() {
dog1.call()
dog2.call()
dog3.call()
// 新增代码
dog4.call()
}
}
class Dog {
call() {
console.log("狗叫")
}
}
let dog1 = new Dog()
let dog2 = new Dog()
let dog3 = new Dog()
// 新增代码:
let dog4 = new Dog()
let thief = new Thief();
thief.action()
// 第二版
// 1、thief增加了list属性,是一个数组
// 2、subscrible方法,追加方法
// 3、publish 发布消息
class Thief {
constructor() {
this.list = []
}
//
subscrible(call) {
this.list.push(call)
}
// publish遍历数组,调用所有方法。
publish() {
for (let i = 0; i < this.list.length; i++) {
this.list[i]()
}
}
// thief的方法内部不会直接调用dog的方法了,
// 而是调用publish
action() {
this.publish()
}
}
class Dog {
call() {
console.log("狗叫")
}
}
let thief = new Thief();
let dog1 = new Dog()
thief.subscrible(dog1.call)
// 每增加一条狗就将狗的call方法追加到list
let dog2 = new Dog()
thief.subscrible(dog2.call)
let dog3 = new Dog()
thief.subscrible(dog3.call)
thief.action()
// 第二版,新增dog4
// 1、thief增加了list属性,是一个数组
// 2、subscrible方法,追加方法
// 3、publish 发布消息
class Thief {
constructor() {
this.list = []
}
//
subscrible(call){
this.list.push(call)
}
// publish遍历数组,调用所有方法。
publish(){
for(let i= 0 ;i<this.list.length;i++){
this.list[i]()
}
}
// thief的方法内部不会直接调用dog的方法了,
// 而是调用publish
action() {
this.publish()
}
}
class Dog {
call() {
console.log("狗叫")
}
}
let thief = new Thief();
let dog1 = new Dog()
thief.subscrible(dog1.call)
// 每增加一条狗就将狗的call方法追加到list
let dog2 = new Dog()
thief.subscrible(dog2.call)
let dog3 = new Dog()
thief.subscrible(dog3.call)
// 增加代码:
let dog4 = new Dog()
thief.subscrible(dog4.call)
thief.action()
// 第二版,新增thief
class Thief {
constructor() {
this.list = []
}
//
subscrible(call){
this.list.push(call)
}
// publish遍历数组,调用所有方法。
publish(){
for(let i= 0 ;i<this.list.length;i++){
this.list[i]()
}
}
// thief的方法内部不会直接调用dog的方法了,
// 而是调用publish
action() {
this.publish()
}
}
class Dog {
call() {
console.log("狗叫")
}
}
let thief = new Thief();
// 新增thief代码
let thief1 = new Thief()
let dog1 = new Dog()
thief.subscrible(dog1.call)
// 新增代码
thief1.subscrible(dog1.call)
let dog2 = new Dog()
thief.subscrible(dog2.call)
// 新增代码
thief1.subscrible(dog2.call)
let dog3 = new Dog()
thief.subscrible(dog3.call)
// 新增代码
thief1.subscrible(dog3.call)
thief.action()
// 新增代码
thief1.action()
class Pubsub{ constructor(){ this.list = [] } subscrible(call){ this.list.push(call) } publish(){ for(let i= 0 ;i<this.list.length;i++){ this.list[i]() } }}
let pubsub = new Pubsub();
class Dog {
call() {
console.log("狗叫")
}
}
class Thief {
constructor() {
}
action() {
pubsub.publish()
}
}
let thief = new Thief();
let dog1 = new Dog()
pubsub.subscrible(dog1.call)
let dog2 = new Dog()
pubsub.subscrible(dog2.call)
let dog3 = new Dog()
pubsub.subscrible(dog3.call)
thief.action()
let pubsub = new Pubsub();
class Dog {
call() {
console.log("狗叫")
}
}
class Thief {
constructor() {
}
action() {
pubsub.publish()
}
}
let thief = new Thief();
// 新增thief1代码
let thief1 = new Thief();
let dog1 = new Dog()
pubsub.subscrible(dog1.call)
let dog2 = new Dog()
pubsub.subscrible(dog2.call)
let dog3 = new Dog()
pubsub.subscrible(dog3.call)
// 新增dog4代码
let dog4 = new Dog()
pubsub.subscrible(dog4.call)
thief.action()
class Pubsub { constructor() { let promise = new Promise((resolve,reject)=>{ this.resolve = resolve; }) this.promise = promise; } subscrible(call) { this.promise.then(call) } publish() { this.resolve(); }}
class Pubsub {
constructor() {
let promise = new Promise((resolve,reject)=>{
this.resolve = resolve;
})
this.promise = promise;
}
subscrible(call) {
this.promise.then(call)
}
publish() {
this.resolve();
}
}
let pubsub = new Pubsub();
class Dog {
call() {
console.log("狗叫")
}
}
class Thief {
constructor() {
}
action() {
pubsub.publish()
}
}
let thief = new Thief();
// 新增thief1代码
let thief1 = new Thief();
let dog1 = new Dog()
pubsub.subscrible(dog1.call)
let dog2 = new Dog()
pubsub.subscrible(dog2.call)
let dog3 = new Dog()
pubsub.subscrible(dog3.call)
// 新增dog4代码
let dog4 = new Dog()
pubsub.subscrible(dog4.call)
thief.action()
const axios = require('axios')// 1、获取CancelTokenvar CancelToken = axios.CancelToken;// 2、生成sourcevar source = CancelToken.source();console.log(source.token)axios.get('/user/12345', {//get请求在第二个参数 // 3、注入source.token cancelToken: source.token}).catch(function (thrown) { console.log(thrown)});axios.post('/user/12345', {//post请求在第三个参数 name: 'new name'}, { cancelToken: source.token}).catch(e => { console.log(e)});// 4、调用source.cancel("原因"),终止注入了source.token的请求source.cancel('不想请求了');
'use strict';var Cancel = require('./Cancel');/** * A `CancelToken` is an object that can be used to request cancellation of an operation. * * @class * @param {Function} executor The executor function. */function CancelToken(executor) { if (typeof executor !== 'function') { throw new TypeError('executor must be a function.'); } var resolvePromise; this.promise = new Promise(function promiseExecutor(resolve) { resolvePromise = resolve; }); var token = this; executor(function cancel(message) { if (token.reason) { // Cancellation has already been requested return; } token.reason = new Cancel(message); resolvePromise(token.reason); });}/** * Throws a `Cancel` if cancellation has been requested. */CancelToken.prototype.throwIfRequested = function throwIfRequested() { if (this.reason) { throw this.reason; }};/** * Returns an object that contains a new `CancelToken` and a function that, when called, * cancels the `CancelToken`. */CancelToken.source = function source() { var cancel; var token = new CancelToken(function executor(c) { cancel = c; }); return { token: token, cancel: cancel };};module.exports = CancelToken;
function CancelToken(executor) {
var resolvePromise;
this.promise = new Promise(function promiseExecutor(resolve) {
resolvePromise = resolve;
});
var token = this;
executor(function cancel(message) {
if (token.reason) {
return;
}
token.reason = message
resolvePromise(token.reason);
});
}
CancelToken.source = function source() {
var cancel;
var token = new CancelToken(function executor(c) {
cancel = c;
});
return {
token: token,
cancel: cancel
};
};
if (config.cancelToken) { // Handle cancellation config.cancelToken.promise.then(function onCanceled(cancel) { if (!request) { return; } request.abort(); reject(cancel); // Clean up request request = null; });}
config.cancelToken.promise.then(function onCanceled(cancel) { if (!request) { return; } request.abort(); reject(cancel); // Clean up request request = null;});
到此,关于“如何用promsie实现观察者模式”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。