我正在寻找一个返回一个已解决的承诺值的函数。优雅地失败绝对是一个好处,但它是一个假定的前提条件,即当函数被调用时,promise 已准备好被解决。
虽然我正在使用webdriver.js 承诺实现,它允许类似于下面的队列操作,但我不想太迷失在队列/链等的语义中。仅出于这个原因,这里有一些伪代码来涵盖我的内容'正在努力实现:
var inputs = [...], outputs;
outputs = inputs.map(function(input){
//queue some async tasks to be performed with input
queue.enqueue(...);
//I can't return the *output* value here yet, naturally, so instead
return promise;
});
//now I'll add another task to the same queue
//this means that by the time this task is run
//the async tasks above would have been executed
//and the promises would be "resolvable"... right?
queue.enqueue(function(){
console.log(outputs); //>an array of promises
console.log(doSomeMagic(outputs)); //>resolved values as needed <<<
});
注意:afaikQ.all()
不会做我所追求的 - 它需要一个承诺数组并返回一个数组的承诺,而不是它的解析值。我很高兴被证明是错误的。