87,907
社区成员
发帖
与我相关
我的任务
分享
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);
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;都重置一把就行了。大佬看下这样是不是完美了。
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啊。
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;
}
});
}
}
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)
整理一下格式,你看看能不能行得通
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)