@@ -1033,29 +1033,12 @@ func ExampleMergeSeq() {
10331033
10341034 myExecutor .Autorun (func () { wg .Go (myExecutor .Run ) })
10351035
1036- sleep := func (d time.Duration ) async.Task {
1037- return func (co * async.Coroutine ) async.Result {
1038- var sig async.Signal
1039- wg .Add (1 ) // Keep track of timers too.
1040- tm := time .AfterFunc (d , func () {
1041- defer wg .Done ()
1042- myExecutor .Spawn (async .Do (sig .Notify ))
1043- })
1044- co .CleanupFunc (func () {
1045- if tm .Stop () {
1046- wg .Done ()
1047- }
1048- })
1049- return co .Await (& sig ).End ()
1050- }
1051- }
1052-
10531036 myExecutor .Spawn (async .MergeSeq (3 , func (yield func (async.Task ) bool ) {
10541037 defer fmt .Println ("done" )
10551038 for n := 1 ; n <= 6 ; n ++ {
10561039 d := time .Duration (n * 100 ) * time .Millisecond
10571040 f := func () { fmt .Println (n ) }
1058- t := sleep ( d ).Then (async .Do (f ))
1041+ t := async . Sleep ( context . Background (), & wg , d ).Then (async .Do (f ))
10591042 if ! yield (t ) {
10601043 return
10611044 }
@@ -1066,13 +1049,13 @@ func ExampleMergeSeq() {
10661049 fmt .Println ("--- SEPARATOR ---" )
10671050
10681051 myExecutor .Spawn (async .Select (
1069- sleep ( 1000 * time .Millisecond ), // Cancel the following task after a period of time .
1052+ async . Sleep ( context . Background (), & wg , 1000 * time .Millisecond ), // Cancel the following task after 1s .
10701053 async .MergeSeq (3 , func (yield func (async.Task ) bool ) {
10711054 defer fmt .Println ("done" )
10721055 for n := 1 ; ; n ++ { // Infinite loop.
10731056 d := time .Duration (n * 100 ) * time .Millisecond
10741057 f := func () { fmt .Println (n ) }
1075- t := sleep ( d ).Then (async .Do (f ))
1058+ t := async . Sleep ( context . Background (), & wg , d ).Then (async .Do (f ))
10761059 if ! yield (t ) {
10771060 return
10781061 }
@@ -1124,23 +1107,6 @@ func Example_panicAndRecover() {
11241107 })
11251108 })
11261109
1127- sleep := func (d time.Duration ) async.Task {
1128- return func (co * async.Coroutine ) async.Result {
1129- var sig async.Signal
1130- wg .Add (1 ) // Keep track of timers too.
1131- tm := time .AfterFunc (d , func () {
1132- defer wg .Done ()
1133- myExecutor .Spawn (async .Do (sig .Notify ))
1134- })
1135- co .CleanupFunc (func () {
1136- if tm .Stop () {
1137- wg .Done ()
1138- }
1139- })
1140- return co .Await (& sig ).End ()
1141- }
1142- }
1143-
11441110 recover := func (co * async.Coroutine ) async.Result {
11451111 fmt .Println (co .Recover ())
11461112 return co .End ()
@@ -1178,7 +1144,7 @@ func Example_panicAndRecover() {
11781144 async .Defer (recover ),
11791145 func (co * async.Coroutine ) async.Result {
11801146 co .Spawn (async .Block (
1181- sleep ( 100 * time .Millisecond ),
1147+ async . Sleep ( context . Background (), & wg , 100 * time .Millisecond ),
11821148 async .Do (func () { panic ("A" ) }), // Panics after 100ms.
11831149 ))
11841150 co .Spawn (async .Block (
@@ -1282,6 +1248,24 @@ func Example_panicAndRecover() {
12821248 wg .Wait ()
12831249 fmt .Println ("--- SEPARATOR ---" )
12841250
1251+ myExecutor .Spawn (async .Join (
1252+ async .Block (
1253+ async .Defer (recover ),
1254+ async .Go (context .Background (), & wg , func (_ context.Context ) async.Task {
1255+ panic ("A" )
1256+ }),
1257+ ),
1258+ async .Block (
1259+ async .Defer (recover ),
1260+ async .Go (context .Background (), & wg , func (_ context.Context ) async.Task {
1261+ return async .Panic ("A" )
1262+ }),
1263+ ),
1264+ ))
1265+
1266+ wg .Wait ()
1267+ fmt .Println ("--- SEPARATOR ---" )
1268+
12851269 myExecutor .Spawn (func (_ * async.Coroutine ) async.Result {
12861270 panic (dummyError ) // Unrecovered panics get repanicked when (*async.Executor).Run returns.
12871271 })
@@ -1314,5 +1298,8 @@ func Example_panicAndRecover() {
13141298 // B
13151299 // <nil>
13161300 // --- SEPARATOR ---
1301+ // A
1302+ // A
1303+ // --- SEPARATOR ---
13171304 // dummy error recovered!
13181305}
0 commit comments