Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Blocking in a deferred function in a promise crashes #1211

Open
@flimzy

Description

@flimzy

Reproduction case:

main.inc.js:

$global.p = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve("foo");
    }, 300);
});

main.go:

package main

import (
	"fmt"

	"github.com/gopherjs/gopherjs/js"
)

func main() {
	p := js.Global.Get("p")
	p.Call("then", func(r *js.Object) {
		defer func() {
			fmt.Println("deferred")
		}()
	}).Call("catch", func(e *js.Object) {
	})
}

When I execute this code, I get the following output:

deferred
/home/jonhall/src/test/test.js:1737
            r();
            ^

TypeError: r is not a function
    at $runScheduled (/home/jonhall/src/test/test.js:1737:13)
    at $schedule (/home/jonhall/src/test/test.js:1761:9)
    at queueEntry (/home/jonhall/src/test/test.js:1834:9)
    at $send (/home/jonhall/src/test/test.js:1793:9)
    at $b (/home/jonhall/src/test/test.js:15011:9)
    at $b (/home/jonhall/src/test/test.js:14318:9)
    at /home/jonhall/src/test/test.js:68:74
    at FSReqCallback.wrapper [as oncomplete] (node:fs:816:5)

Hypothesis: The rule that blocking code in a callback must be executed in a goroutine should apply to deferred functions within callbacks as well. If I move the fmt.Println call to a goroutine within the deferred function, the error goes away.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions