go-promise icon indicating copy to clipboard operation
go-promise copied to clipboard

Return channel on promise.Resolve

Open petersondmg opened this issue 7 years ago • 2 comments

I think it would be great being able to wait for Resolve method to actually run its callbacks, or at least to have two methods like Resolve and ResolveAsync.

I'd expect the following code to print "running callback" but this doesn't happen since promise.Resolve is using a go routine and this is not clear.

package main

import "github.com/fanliao/go-promise"
import "fmt"

func main() {
        p := promise.NewPromise()
        p.OnSuccess(func(v interface{}) {
                fmt.Println("running callback")
        })
        p.Resolve(nil)
}

Thank you!

petersondmg avatar Mar 08 '17 14:03 petersondmg

I think that does not happen because the application closes before the goroutine runs. Try to put a time.Sleep(time.Second) after the p.Resolve. It should print OnSuccess.

racerxdl avatar Feb 12 '19 13:02 racerxdl

Btw, that might happen on Nodejs as well. If the application finishes, it does not wait for all promises to execute their callbacks.

racerxdl avatar Feb 12 '19 13:02 racerxdl