문제

I am having trouble wrapping my head around how to make a Dojo Deferred work for the following scenario. Basically I have a function that is supposed to loop through a number of items and process each item separately in a deferred manner. It does the processing of each item in a separate method that returns a Deferred when it is done. I want to be able to execute the loop to go off and process each item and report back a success or failure when it is done. Here is what I got so far as a test sample:

http://jsfiddle.net/Ayyoudy/mgv6Y/12/

What am I doing wrong? I am expecting the output numbers to be in sequence but they are not.

I also want to be able to detect if there is an error processing one of the items and if so, not to process the rest.

Thanks

도움이 되었습니까?

해결책

  1. I have resolve that the output number should be in sequence. http://jsfiddle.net/granjoy/mgv6Y/17/ There're couple of issues in your code

    • loopping function should use closure to hold the variable.
    • You should use the validate returned Deferred now the process Deferred
  2. Given that process() function will finished immediately, so the log "done processing everything!" will be first output.

Edit:

I have learn from the question. Thanks, the author gives another good solution. http://jsfiddle.net/Ayyoudy/mgv6Y/14

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top