この例は、どのように機能するかについての私の理解を混乱させますnode.js
。
// 1.
numbers.forEach(function(number) {
queue.push(Q.call(slowFunction, this, number));
});
// 2.
// Q.all: execute an array of 'promises' and 'then' call either a resolve
// callback (fulfilled promises) or reject callback (rejected promises)
Q.all(queue).then(function(ful) {
// All the results from Q.all are on the argument as an array
console.log('fulfilled', ful);
}, function(rej) {
// The first rejected (error thrown) will be here only
console.log('rejected', rej);
}).fail(function(err) {
// If something went wrong, then we catch it here, usually when there is no
// rejected callback.
console.log('fail', err);
}).fin(function() {
// Finally statement; executed no matter of the above results
console.log('finally');
});
ここで、コードの一部が順番に実行される1.
と想定されるのはなぜですか?では、プッシュされたすべての要素2.
で機能する保証はどこにありますか?それは、fromが非常に大きく、並列よりも機能するということでしょうか?これらのアイデアは、node.jsが最初にを処理し、次にそれをに与えるという理解から生まれました。これは実際には通常のスレッドに類似しています。Q.all(queue)
queue
1.
numbers
1.
2.
1.
2.
node.js event-loop
workers
したがって、質問は、互いに並行して実行され、1.
順番に実行されますか、それとも順番に実行されますか(キュー内のすべての要素をプッシュし、その後でのみ、の各要素の処理を開始します)?このトピックのドキュメントへの直接リンクを引数に指定してください。2.
node.js event-loop
1.
2.
queue