Code Monkey home page Code Monkey logo

Comments (4)

fantasy0v0 avatar fantasy0v0 commented on June 2, 2024

then of io.reactiverse.es4x.Thenable Maybe should return new Future.

image

from es4x.

fantasy0v0 avatar fantasy0v0 commented on June 2, 2024

then of io.reactiverse.es4x.Thenable Maybe should return new Future.

image

I tried to modify it, but the problem was not simple.😣https://promisesaplus.com/#the-then-method

from es4x.

fantasy0v0 avatar fantasy0v0 commented on June 2, 2024

I try to implement this with native promises. I testing.

@Override
  public Value then(Value onResolve, Value onReject) {
    try {
      return JavaScript.createPromise(context, (_resolve, _reject) -> {
        Consumer<Object> onResolveNext = value -> {
          if (value instanceof Value _value) {
            if (JavaScript.isPromise(_value)) {
              _value.invokeMember("then", _resolve, _reject);
            } else {
              _resolve.execute(value);
            }
          } else {
            _resolve.execute(value);
          }
        };
        Consumer<Object> onRejectNext = err -> {
          if (err instanceof Value _value) {
            if (JavaScript.isPromise(_value)) {
              _value.invokeMember("then", _resolve, _reject);
            } else {
              _reject.execute(err);
            }
          } else {
            _reject.execute(err);
          }
        };
        future.onComplete(ar -> {
          if (ar.succeeded()) {
            T result = ar.result();
            if (onResolve != null && onResolve.canExecute()) {
              Value value = onResolve.execute(result);
              onResolveNext.accept(value);
            } else {
              _resolve.execute(result);
            }
          } else {
            Throwable cause = ar.cause();
            if (onReject != null && onReject.canExecute()) {
              Value value = onReject.execute(cause);
              onRejectNext.accept(value);
            } else {
              _reject.execute(cause);
            }
          }
        });
      });
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

JavaScript.createPromise implement:

public static Value createPromise(Context context, PromiseExecutor executor) throws IOException {
    Value bindings = context.getBindings("js");
    bindings.putMember("executor", executor);
    try {
      Source source = Source
        .newBuilder("js", "new Promise(executor)", null)
        .build();
      return context.eval(source);
    } finally {
      bindings.removeMember("executor");
    }
  }

from es4x.

pmlopes avatar pmlopes commented on June 2, 2024

@fantasy0v0 yes, that is the vert.x behavior, JavaScript has a similar behavior:

let myPromise = new Promise(function(myResolve, myReject) {
  myResolve('OK'); // when successful
  myResolve('OK'); // when successful
});

// "Consuming Code" (Must wait for a fulfilled Promise)
myPromise.then(
  function(value) { console.log(value); },
  function(error) { console.log(error); }
);

With a small difference that calling multiple times will silently ignore the multiple calls. Let's update es4x to be like that, ignore the multiple calls.

from es4x.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.