有没有清除.thenJavaScript Promise实例的方法?
我在QUnit上编写了一个JavaScript测试框架.框架通过在a中运行每个框架来同步运行测试Promise.(抱歉这个代码块的长度.我尽可能地评论它,所以感觉不那么乏味.)
/* Promise extension -- used for easily making an async step with a
timeout without the Promise knowing anything about the function
it's waiting on */
$$.extend(Promise, {
asyncTimeout: function (timeToLive, errorMessage) {
var error = new Error(errorMessage || "Operation timed out.");
var res, // resolve()
rej, // reject()
t, // timeout instance
rst, // reset timeout function
p, // the promise instance
at; // the returned asyncTimeout instance
function …Run Code Online (Sandbox Code Playgroud) 我正在使用带有es7风格的异步/等待方法的babeljs.我有一个主脚本,它将在所有返回promises的对象数组上调用异步方法.我使用Promise.all()等待所有这些返回,但是,这些任务可能需要很长时间,如果它们超过阈值,我想中止所有这些,并且任务以适当的方式处理.
反正有没有完成这样的事情?目前,我能想到的唯一方法是生成一个进程,该进程执行调用这些方法的工作并等待它们全部解决,如果达到时间限制,它可以终止进程并执行它需要的任何处理.
更新:关于主脚本正在等待的这些方法的一些说明......他们可能正在进行一系列的操作(调用外部系统,某些地方的流文件等)而不执行可以独立取消的单个操作.
更新#2:一些未经测试的半伪代码
class Foo1 {
async doSomething() {
// call some external system
// copy some files
// put those files somewhere else (s3)
}
}
class Foo2 {
async doSomething() {
// Do some long computations
// Update some systems
}
}
class FooHandler {
constructor() {
this.fooList = [];
}
async start() {
await Promise.all(this.fooList.map(async (foo) => {
return await foo.doSomething();
}));
}
}
let handler = new FooHandler();
handler.fooList.push(new Foo1());
handler.fooList.push(new Foo2());
// if this …Run Code Online (Sandbox Code Playgroud) 我希望程序在完成某些用户操作后运行一系列操作。然而,链的一部分将需要等待先前 Promise 的解决或用户已执行某些操作的事实。Promise 可以这样工作吗?
我想象理想的程序脚本是这样的:
var coreTrigger = Promise.any([normalAsyncRequest, userAction]);
coreTrigger.then(res=>{
// the followup action
});
...
// somewhere far away, or in developer console
userAction.done(); // I want this can be one possible path to trigger the followup action
Run Code Online (Sandbox Code Playgroud) javascript ×3
promise ×2
asynchronous ×1
babeljs ×1
cancellation ×1
ecmascript-7 ×1
es6-promise ×1
node.js ×1