These are chat archives for opal/opal

17th
Mar 2018
W.
@graywolf
Mar 17 2018 14:10
assuming, I want to resolve promise from Javascript, can I do anything better then:
Opal.send(promise, "resolve", [data]);
? In particular, why I need the array as third argument? So with two it would look like Opal.send(promise, "resolve", [data1, data2]);?
Elia Schito
@elia
Mar 17 2018 14:14
@graywolf promise.$resolve(data)
Ilya Bylich
@iliabylich
Mar 17 2018 14:14
promise.$resolve(data) should be enough
:smile:
Elia Schito
@elia
Mar 17 2018 14:14
:smile:
W.
@graywolf
Mar 17 2018 14:31
I love you guys :) Opal is soo cool project
Elia Schito
@elia
Mar 17 2018 14:31
:blush:
W.
@graywolf
Mar 17 2018 14:42
hm, I have another issue. When I try to use native object without wraping in Native(), the execution just stops... no error message or anything, is that expected or should I make bug report?
Elia Schito
@elia
Mar 17 2018 14:44
@graywolf need more info on how the problem is generated, what code translates "When I try to use native" etc.
W.
@graywolf
Mar 17 2018 14:47

Sure.

    @wrap = `chrome.storage.sync`
      def get(key)
        promise = Promise.new
        %x{
          #{@wrap}.get(key, function(data) {
            promise.$resolve(data);
          });
        }
        promise
      end

Calling code is

  Opal::Chrome::Storage.sync.get('test').then do |x|
    `console.log(x);`
    x = Native(x)
    puts x.class
    puts x.test
    puts x['test']
    i ||= 1
    Opal::Chrome::Storage.sync.set(test: 2)
    `console.log('set called');`
  end.then do |x|
    `console.log('set done');`
  end

Like this it produces this in console:

Object
sys.min.js:1 Native::Object
2
sys.min.js:1 2
sys.min.js:1 {"test"=>2}
popup.min.js:1 set called
popup.min.js:1 set done

but when I comment the x = Native(x) out all I get is:

Object

so the execution seems to stop on line puts x.class however there is no
error message or anything

Elia Schito
@elia
Mar 17 2018 14:48
@graywolf that's because you're using a promise but not defining a .fail { … }block for failures
W.
@graywolf
Mar 17 2018 14:49
oh, so without .fail the exceptions in promise block are just swalled silently?
Elia Schito
@elia
Mar 17 2018 14:49
   …
  end.then do |x|
    `console.log('set done');`
  end.fail {|err| `console.log(['error', err])`}
yep
W.
@graywolf
Mar 17 2018 14:51
oh, didn't know :) well now that I know it hopefully won't bite me next time :)
would PR adding option to disable this behaviour be accepted or is this considered core feature of promise?
Elia Schito
@elia
Mar 17 2018 14:57
@graywolf no, having it as an option is fine and desirable, would be good if it had an interface similar to Thread with abort_on_exception
W.
@graywolf
Mar 17 2018 14:58
will try to put something together then :)
Jamie Gaskins
@jgaskins
Mar 17 2018 22:47

@graywolf The only way I've ever gotten exceptions in promises to bubble up is by using requestAnimationFrame to pull it out of the promise stack:

promise.then { |value| `requestAnimationFrame(() => do_something_with(value))` }

You lose some of the chainability that promises give you (that is, the chained promise wouldn't be able to get the result of do_something_with(value) because it's executing outside the then block), but it's really helpful in debugging exceptions.

Elia Schito
@elia
Mar 17 2018 22:49

@graywolf @jgaskins on the same line I have this in my core-ext.js.rb:

require 'promise'
class Promise
  def exception!(error)
    @exception = true
    $console.error(:exception_in_promise, error, self) if $DEBUG
    reject!(error)
  end

  def pending?
    !@realized
  end
end

maybe it can be combined with requestAnimationFrame to get the live exception instead of just the log