查看 javascript 中的所有未决承诺
View all pending promises in javascript
在我的测试中,有时我会超时,查看在超时之前未决的 promise 在哪里非常有用,这样我就知道哪些 promise 最有可能出现在 "always pending state".
有办法吗?
这是一个示例代码:
Promise.resolve().then(function firstFunction() {
console.log(1);
return 1;
}).then(function () {
return new Promise(function secondFunction(resolve, reject) {
// NEVER RESOLVING PROMISE
console.log(2);
});
}).then(function thirdFunction() {
// function that will never be called
console.log(3);
})
setTimeout(function timeoutTest() {
const pendingPromises = [];// ??????????? how do I get the pendingPromises
console.log(pendingPromises);
process.exit();
}, 5000);
如果可能的话,我想在 pendingPromises
中获取承诺 secondFunction
的函数名称和堆栈跟踪,因为它永远不会解析。
我建议使用 Bluebird 之类的库,它比本机承诺快 6 倍,提供有用的警告和其他有用的方法,例如 - 超时,这可能会帮助您解决这个问题。
Promise 链被设计为在其成功路径或 "reason" 错误路径中逐步传递价值。它的设计目的不是为了在某个任意时间点查询 "snapshot" 被它吸收的单个承诺的状态。
因此,promise 链不提供 "natural" 方法来完成您的要求。
您需要为 :
设计一种机制
- 注册兴趣承诺。
- 跟踪每个已注册承诺的状态(如果承诺实现尚未提供)。
- 按需检索已注册的承诺,按状态过滤。
沿着这些路线的构造函数将完成这项工作:
function Inspector() {
var arr = [];
this.add = function(p, label) {
p.label = label || '';
if(!p.state) {
p.state = 'pending';
p.then(
function(val) { p.state = 'resolved'; },
function(e) { p.state = 'rejected'; }
);
}
arr.push(p);
return p;
};
this.getPending = function() {
return arr.filter(function(p) { return p.state === 'pending'; });
};
this.getSettled = function() {
return arr.filter(function(p) { return p.state !== 'pending'; });
};
this.getResolved = function() {
return arr.filter(function(p) { return p.state === 'resolved'; });
};
this.getRejected = function() {
return arr.filter(function(p) { return p.state === 'rejected'; });
};
this.getAll = function() {
return arr.slice(0); // return a copy of arr, not arr itself.
};
};
题目要求的方法只有.add()
和.getPending()
。提供其他的是为了完整性。
您现在可以写:
var inspector = new Inspector();
Promise.resolve().then(function() {
console.log(1);
}).then(function() {
var p = new Promise(function(resolve, reject) {
// NEVER RESOLVING PROMISE
console.log(2);
});
return inspector.add(p, '2');
}).then(function() {
// function that will never be called
console.log(3);
});
setTimeout(function() {
const pendingPromises = inspector.getPending();
console.log(pendingPromises);
process.exit();
}, 5000);
Inspector
的使用并不局限于被 promise 链同化的 promise。它可以用于任意一组承诺,例如一组要与 Promise.all()
聚合:
promise_1 = ...;
promise_2 = ...;
promise_3 = ...;
var inspector = new Inspector();
inspector.add(promise_1, 'promise 1');
inspector.add(promise_2, 'promise 2');
inspector.add(promise_3, 'promise 3');
var start = Date.now();
var intervalRef = setInterval(function() {
console.log(Date.now() - start + ': ' + inspector.getSettled().length + ' of ' + inspector.getAll().length + ' promises have settled');
}, 50);
Promise.all(inspector.getAll()).then(successHandler).catch(errorHandler).finally(function() {
clearInterval(intervalRef);
});
在我的测试中,有时我会超时,查看在超时之前未决的 promise 在哪里非常有用,这样我就知道哪些 promise 最有可能出现在 "always pending state".
有办法吗?
这是一个示例代码:
Promise.resolve().then(function firstFunction() {
console.log(1);
return 1;
}).then(function () {
return new Promise(function secondFunction(resolve, reject) {
// NEVER RESOLVING PROMISE
console.log(2);
});
}).then(function thirdFunction() {
// function that will never be called
console.log(3);
})
setTimeout(function timeoutTest() {
const pendingPromises = [];// ??????????? how do I get the pendingPromises
console.log(pendingPromises);
process.exit();
}, 5000);
如果可能的话,我想在 pendingPromises
中获取承诺 secondFunction
的函数名称和堆栈跟踪,因为它永远不会解析。
我建议使用 Bluebird 之类的库,它比本机承诺快 6 倍,提供有用的警告和其他有用的方法,例如 - 超时,这可能会帮助您解决这个问题。
Promise 链被设计为在其成功路径或 "reason" 错误路径中逐步传递价值。它的设计目的不是为了在某个任意时间点查询 "snapshot" 被它吸收的单个承诺的状态。
因此,promise 链不提供 "natural" 方法来完成您的要求。
您需要为 :
设计一种机制- 注册兴趣承诺。
- 跟踪每个已注册承诺的状态(如果承诺实现尚未提供)。
- 按需检索已注册的承诺,按状态过滤。
沿着这些路线的构造函数将完成这项工作:
function Inspector() {
var arr = [];
this.add = function(p, label) {
p.label = label || '';
if(!p.state) {
p.state = 'pending';
p.then(
function(val) { p.state = 'resolved'; },
function(e) { p.state = 'rejected'; }
);
}
arr.push(p);
return p;
};
this.getPending = function() {
return arr.filter(function(p) { return p.state === 'pending'; });
};
this.getSettled = function() {
return arr.filter(function(p) { return p.state !== 'pending'; });
};
this.getResolved = function() {
return arr.filter(function(p) { return p.state === 'resolved'; });
};
this.getRejected = function() {
return arr.filter(function(p) { return p.state === 'rejected'; });
};
this.getAll = function() {
return arr.slice(0); // return a copy of arr, not arr itself.
};
};
题目要求的方法只有.add()
和.getPending()
。提供其他的是为了完整性。
您现在可以写:
var inspector = new Inspector();
Promise.resolve().then(function() {
console.log(1);
}).then(function() {
var p = new Promise(function(resolve, reject) {
// NEVER RESOLVING PROMISE
console.log(2);
});
return inspector.add(p, '2');
}).then(function() {
// function that will never be called
console.log(3);
});
setTimeout(function() {
const pendingPromises = inspector.getPending();
console.log(pendingPromises);
process.exit();
}, 5000);
Inspector
的使用并不局限于被 promise 链同化的 promise。它可以用于任意一组承诺,例如一组要与 Promise.all()
聚合:
promise_1 = ...;
promise_2 = ...;
promise_3 = ...;
var inspector = new Inspector();
inspector.add(promise_1, 'promise 1');
inspector.add(promise_2, 'promise 2');
inspector.add(promise_3, 'promise 3');
var start = Date.now();
var intervalRef = setInterval(function() {
console.log(Date.now() - start + ': ' + inspector.getSettled().length + ' of ' + inspector.getAll().length + ' promises have settled');
}, 50);
Promise.all(inspector.getAll()).then(successHandler).catch(errorHandler).finally(function() {
clearInterval(intervalRef);
});