Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise.wait() blocks setTimeout #229

Closed
oravecz opened this issue May 4, 2013 · 2 comments
Closed

Promise.wait() blocks setTimeout #229

oravecz opened this issue May 4, 2013 · 2 comments
Labels

Comments

@oravecz
Copy link
Contributor

oravecz commented May 4, 2013

I wasn't sure what to type for the subject as this isn't quite a bug or an enhancement request. It's just a behavior that could be changed perhaps.

var {Deferred} = require( 'ringo/promise' );

var deferred = new Deferred();

setTimeout( function () {
    deferred.resolve( 'hello world' );
}, 1000 );

var value = deferred.promise.wait( 5000 );

print( 'Value: ' + value );

When executed, the above code prints out "Value: undefined" after a 5 second pause. I was expecting it to print out "Value: hello world" after a one second pause.

My expectation is that the deferred.wait() would be interrupted by the deferred.resolve() in the setTimeout handler. But instead deferred.wait() blocks the setTimeout countdown timer because it is also on the main event thread.

The following code illustrates the affect I was hoping to achieve.

var {Deferred} = require( 'ringo/promise' );

var deferred = new Deferred();

function newTimeout( f, delay ) {
    new java.lang.Thread( new java.lang.Runnable({
        run: function() {
            java.lang.Thread.sleep(delay);
            f.call(this);
        }
    }) ).start();
}

newTimeout( function () {
    deferred.resolve( 'hello world' );
}, 1000 );

var value = deferred.promise.wait( 5000 );

print( 'Value: ' + value );

There would be issues with this naive replacement for setTimeout, particularly the fact that the concept of 'this' is changed. Calling new Timeout like this would solve that problem:

newTimeout( function () {
    deferred.resolve( 'hello world' );
}.bind(this), 1000 );

So, I'm not particularly suggesting an enhancement or pointing out a defect. There may be very good reasons why setTimeout has to be run on the main event loop.

The inclusion of Promise.wait() is a very nice option when blocking is required, so I am glad to have it available, even if it requires some additional knowledge regarding its use.

@oberhamsi
Copy link
Member

there is something fishy about our promise.wait(). it's built on the assumption that the promise is coming from another worker because only then can it ever resolve. but we actually suggest - but don't enforce - strict worker seperation.

the following behaviour is also about event loops vs threads. we need to figure this out. promises-aplus/promises-spec#68 (comment)

@botic
Copy link
Member

botic commented Aug 11, 2020

I’m closing this issue because it has been inactive for a long time. This probably means that it is not reproducible or it has been fixed in a newer version. If it’s an enhancement and hasn’t been taken on for so long, then it seems no one has the time to implement this / there is no interest in it.

Please reopen if you still encounter this issue with the current master version or there is a need to address this issue.

@botic botic closed this as completed Aug 11, 2020
@botic botic added the wontfix label Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants