このコードは12334または12433を返す必要がありますか?
私は12334を期待しますが、それは12433を与えます...
console.log '1'
anArray.forEach (info, index, array)->
console.log '2'
await model.findOne info, defer(err, doc)
console.log '3'
console.log '4'
このコードは12334または12433を返す必要がありますか?
私は12334を期待しますが、それは12433を与えます...
console.log '1'
anArray.forEach (info, index, array)->
console.log '2'
await model.findOne info, defer(err, doc)
console.log '3'
console.log '4'
あなたの直感は正しくありません:IcedCoffeeScriptawait...defer
は同期forEach
ループをブロックできません。
IcedCoffeeScriptは、ブロッキングをサポートしていないJavaScriptにコンパイルされることに注意してください。配列を呼び出したらforEach
、イベントが発生する前に、その配列全体を反復処理することを約束します。
幸いなことに、IcedCoffeeScript独自のループ構造を使用することで、必要な動作を得ることができます。あなたの場合、
for info, index in anArray ->
...
JavaScriptの非ブロッキングイベントモデルの処理の詳細については、私の新しい本、AsyncJavaScriptをチェックしてください。