您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。