promise的取消

讨厌走开啦
博客专家认证
2019-03-26 09:36:45
有2个promise分别为promiseA和promiseB,promiseA先创建,promiseB后创建,怎样处理才能达到以下效果:

如果promiseA在执行then时promiseB还没有创建,则正常执行promiseA的then处理逻辑;
如果promiseA在执行then时promiseB已经创建,则不执行promiseA的then处理逻辑;
promiseA和promiseB的then执行是无须的(即有可能promiseB后创建,但是promiseB先执行完走到promiseB的then)。
...全文
1390 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
jetmiller 2019-03-28
  • 打赏
  • 举报
回复
可以的,受教了
澄ོ殇ꦿ℘゜ 2019-03-27
  • 打赏
  • 举报
回复
如果promiseA在执行then时promiseB还没有创建,则正常执行promiseA的then处理逻辑; 如果promiseA在执行then时promiseB已经创建,则不执行promiseA的then处理逻辑; promiseA和promiseB的then执行是无须的(即有可能promiseB后创建,但是promiseB先执行完走到promiseB的then)。
澄ོ殇ꦿ℘゜ 2019-03-27
  • 打赏
  • 举报
回复
有2个promise分别为promiseA和promiseB,promiseA先创建,promiseB后创建,怎样处理才能达到以下效果:
谷农 2019-03-27
  • 打赏
  • 举报
回复
什么时候有分数啊
2019-03-26
  • 打赏
  • 举报
回复
这个应该可以吧

class MutexPromise
{
	constructor(then){
		this.then = then;
		this.promise = null;
	}

	add(promise){
		this.promise = promise;

		promise.then((data) => {
			if(promise === this.promise){
				this.then(data);
				this.promise = null;
			}else{
				console.log("放弃", data);
			}
		});
	}
}

var thenCallback = (data) => {
    console.log(data, "callback");
};
var mutex = new MutexPromise(thenCallback);
mutex.add(new Promise((resolve, reject) => {
	console.log("查询 普通妹子");
	setTimeout(() => {
		resolve("普通妹子");
	}, Math.random() * 3000)
}), thenCallback);

setTimeout(() => {
	mutex.add(new Promise((resolve, reject) => {
		console.log("查询 漂亮妹子");
		setTimeout(() => {
			resolve("漂亮妹子");
		}, Math.random() * 2000)
	}), thenCallback);
}, Math.random() * 2000);

setTimeout(() => {
	mutex.add(new Promise((resolve, reject) => {
		console.log("查询 女汉子");
		setTimeout(() => {
			resolve("女汉子");
		}, Math.random() * 2000)
	}), thenCallback);
}, Math.random() * 3000);
讨厌走开啦 2019-03-26
  • 打赏
  • 举报
回复
引用 21 楼 讨厌走开啦 的回复:
[quote=引用 20 楼 囧 的回复:] [quote=引用 14 楼 讨厌走开啦 的回复:] [quote=引用 11 楼 囧 的回复:]

class MutexPromise
{
	constructor(){
		this.then = null;
		this.isSuccess = false;
	}

	add(promise, then){
		this.then = then;
		promise._then = then;
		promise.then((data) => {
			if(!this.isSuccess && promise._then === this.then){
				this.then(data);
				this.isSuccess = true;
			}
		});
	}
}
这样不行吧,promise的then都是复用的,也就是说promise._then === this.then 这里肯定是true。[/quote] 不一定是true, _then和promise是配套的,this.then是最后一个,就是判断要执行的then是不是最后一个,写得有些丑[/quote]

    let thenCallback = () => {
      console.log("callback");
    };
    mutex.add(new Promise((resolve, reject) => {console.log("promiseA")}), thenCallback);
    mutex.add(new Promise((resolve, reject) => {console.log("promiseA")}), thenCallback);
then复用的话是这样传进来的,===是true啊。[/quote] 有办法了,当最后一个请求成功的时候,就把this.promise = []; this.then = []; this.isSuccess = false;都重置一把就行了。大佬看下这样是不是完美了。
讨厌走开啦 2019-03-26
  • 打赏
  • 举报
回复
引用 20 楼 囧 的回复:
[quote=引用 14 楼 讨厌走开啦 的回复:] [quote=引用 11 楼 囧 的回复:]

class MutexPromise
{
	constructor(){
		this.then = null;
		this.isSuccess = false;
	}

	add(promise, then){
		this.then = then;
		promise._then = then;
		promise.then((data) => {
			if(!this.isSuccess && promise._then === this.then){
				this.then(data);
				this.isSuccess = true;
			}
		});
	}
}
这样不行吧,promise的then都是复用的,也就是说promise._then === this.then 这里肯定是true。[/quote] 不一定是true, _then和promise是配套的,this.then是最后一个,就是判断要执行的then是不是最后一个,写得有些丑[/quote]

    let thenCallback = () => {
      console.log("callback");
    };
    mutex.add(new Promise((resolve, reject) => {console.log("promiseA")}), thenCallback);
    mutex.add(new Promise((resolve, reject) => {console.log("promiseA")}), thenCallback);
then复用的话是这样传进来的,===是true啊。
2019-03-26
  • 打赏
  • 举报
回复
引用 14 楼 讨厌走开啦 的回复:
[quote=引用 11 楼 囧 的回复:]

class MutexPromise
{
	constructor(){
		this.then = null;
		this.isSuccess = false;
	}

	add(promise, then){
		this.then = then;
		promise._then = then;
		promise.then((data) => {
			if(!this.isSuccess && promise._then === this.then){
				this.then(data);
				this.isSuccess = true;
			}
		});
	}
}
这样不行吧,promise的then都是复用的,也就是说promise._then === this.then 这里肯定是true。[/quote] 不一定是true, _then和promise是配套的,this.then是最后一个,就是判断要执行的then是不是最后一个,写得有些丑
讨厌走开啦 2019-03-26
  • 打赏
  • 举报
回复
引用 17 楼 丰云 的回复:
换个思路吧。。。。 这个做法,一看就不靠谱。。。
有这样的应用场景啊,举个例子,你查询了一个请求“我要妹子”,然后这个请求要花10秒,然后过了2秒你发现,我靠不对,这结果不是我要的,改了下请求“我要漂亮妹子”,然后这个请求要花3秒,如果按正常的处理逻辑,你会发现5秒的时候你要的漂亮妹子来了,然后刚要看大图的时候到10秒了,前一个请求的响应过来给你刷成了普通妹子,你会不会想打人?
讨厌走开啦 2019-03-26
  • 打赏
  • 举报
回复
引用 16 楼 風灬雲 的回复:
[quote=引用 15 楼 讨厌走开啦 的回复:] [quote=引用 13 楼 風灬雲 的回复:] [quote=引用 12 楼 讨厌走开啦 的回复:] [quote=引用 10 楼 風灬雲 的回复:] 好吧 思路和4楼一样
promiseIndex会一直增加。如果能把不执行的promise在响应时从promiseIndex减掉就完美了。[/quote] 如果考虑性能的话 可以在构造函数里面加一个timer 保持 定时器;在回调里面判断this.promiseRun;如果已经执行完成,清除掉定时器就可以了;这样就不会再进行add[/quote] 每次addPromise都加一个定时器吗?那万一接口查询用了10分钟,我每秒发一个请求,不是会起600个定时器?[/quote] 不是= = 是在开始的时候设定定时器;定时器里面执行addPromise[/quote] 你之前说的是 this.promiseRun 如果已经执行完成,清除掉定时器就可以了 那不是发2个请求定时器就关了,后面再来请求怎么办,不重新拉一个新的定时器吗?
丰云 2019-03-26
  • 打赏
  • 举报
回复
换个思路吧。。。。 这个做法,一看就不靠谱。。。
風灬雲 2019-03-26
  • 打赏
  • 举报
回复
引用 15 楼 讨厌走开啦 的回复:
[quote=引用 13 楼 風灬雲 的回复:] [quote=引用 12 楼 讨厌走开啦 的回复:] [quote=引用 10 楼 風灬雲 的回复:] 好吧 思路和4楼一样
promiseIndex会一直增加。如果能把不执行的promise在响应时从promiseIndex减掉就完美了。[/quote] 如果考虑性能的话 可以在构造函数里面加一个timer 保持 定时器;在回调里面判断this.promiseRun;如果已经执行完成,清除掉定时器就可以了;这样就不会再进行add[/quote] 每次addPromise都加一个定时器吗?那万一接口查询用了10分钟,我每秒发一个请求,不是会起600个定时器?[/quote] 不是= = 是在开始的时候设定定时器;定时器里面执行addPromise
讨厌走开啦 2019-03-26
  • 打赏
  • 举报
回复
引用 13 楼 風灬雲 的回复:
[quote=引用 12 楼 讨厌走开啦 的回复:] [quote=引用 10 楼 風灬雲 的回复:] 好吧 思路和4楼一样
promiseIndex会一直增加。如果能把不执行的promise在响应时从promiseIndex减掉就完美了。[/quote] 如果考虑性能的话 可以在构造函数里面加一个timer 保持 定时器;在回调里面判断this.promiseRun;如果已经执行完成,清除掉定时器就可以了;这样就不会再进行add[/quote] 每次addPromise都加一个定时器吗?那万一接口查询用了10分钟,我每秒发一个请求,不是会起600个定时器?
讨厌走开啦 2019-03-26
  • 打赏
  • 举报
回复
引用 11 楼 囧 的回复:

class MutexPromise
{
	constructor(){
		this.then = null;
		this.isSuccess = false;
	}

	add(promise, then){
		this.then = then;
		promise._then = then;
		promise.then((data) => {
			if(!this.isSuccess && promise._then === this.then){
				this.then(data);
				this.isSuccess = true;
			}
		});
	}
}
这样不行吧,promise的then都是复用的,也就是说promise._then === this.then 这里肯定是true。
風灬雲 2019-03-26
  • 打赏
  • 举报
回复
引用 12 楼 讨厌走开啦 的回复:
[quote=引用 10 楼 風灬雲 的回复:] 好吧 思路和4楼一样
promiseIndex会一直增加。如果能把不执行的promise在响应时从promiseIndex减掉就完美了。[/quote] 如果考虑性能的话 可以在构造函数里面加一个timer 保持 定时器;在回调里面判断this.promiseRun;如果已经执行完成,清除掉定时器就可以了;这样就不会再进行add
讨厌走开啦 2019-03-26
  • 打赏
  • 举报
回复
引用 10 楼 風灬雲 的回复:
好吧 思路和4楼一样
promiseIndex会一直增加。如果能把不执行的promise在响应时从promiseIndex减掉就完美了。
2019-03-26
  • 打赏
  • 举报
回复

class MutexPromise
{
	constructor(){
		this.then = null;
		this.isSuccess = false;
	}

	add(promise, then){
		this.then = then;
		promise._then = then;
		promise.then((data) => {
			if(!this.isSuccess && promise._then === this.then){
				this.then(data);
				this.isSuccess = true;
			}
		});
	}
}
風灬雲 2019-03-26
  • 打赏
  • 举报
回复
好吧 思路和4楼一样
風灬雲 2019-03-26
  • 打赏
  • 举报
回复

function Test(){
      this.promiseIndex=0;
      this.promiseRun=false
      this.addPromise=function(promise,then){
            this.promiseIndex+=1
        var index=this.promiseIndex
            if(!this.promiseRun){
               new Promise(promise).then(()=>{
                             if(index===this.promiseIndex&&!this.promiseRun){
                                    this.promiseRun=true;
                                     then()
                              }else{
                                       console.log("后边已经添加了新的promise,可以不用执行then了")
                               }
                            })
            }else{
                  console.log("前面的promise已经执行完了,不用再添加了")
            }
      }
}
var demo=new Test();
demo.addPromise((resolve,reject)=>{
    setTimeout(()=>{resolve()},3000)
},()=>{console.log(1)})
setTimeout(()=>{
    demo.addPromise((resolve,reject)=>{
    setTimeout(()=>{resolve()},1000)
},()=>{console.log(2)})
},2000)
整理一下格式,你看看能不能行得通
風灬雲 2019-03-26
  • 打赏
  • 举报
回复

function Test(){
      this.promiseIndex=0;
      this.promiseRun=false
      this.addPromise=function(promise,then){
            this.promiseIndex+=1
	    var index=this.promiseIndex
            if(!this.promiseRun){
               new Promise(promise).then(()=>{
                                                               if(index===this.promiseIndex&&!this.promiseRun){
                                                                    this.promiseRun=true;
                                                                    then()
                                                               }else{
                                                                    console.log("后边已经添加了新的promise,可以不用执行then了")
                                                               }
							})
            }else{
                  console.log("前面的promise已经执行完了,不用再添加了")
            }
      }
}
var demo=new Test();
demo.addPromise((resolve,reject)=>{
    setTimeout(()=>{resolve()},3000)
},()=>{console.log(1)})
setTimeout(()=>{
	demo.addPromise((resolve,reject)=>{
    setTimeout(()=>{resolve()},1000)
},()=>{console.log(2)})
},2000)
加载更多回复(10)

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧