Skip to content

bautajs-core: add decorator like parallel but that resolves Promise.race #71

Open
@Xavier-Redondo

Description

race decorator for Promise.race

It should be nice to have a decorator race that resolves against promise.race instead of promise.all. this would allow to implement easily services that allow timeout by themselves (instead of waiting for a request to a 3rd party system to timeout).

for example (assume below code is in a resolver):

function firstPipeline() {
  return step(async (_, ctx) => {
    return new Promise(resolve =>
      setTimeout(() => {
        resolve('First pipeline returns this');
      }, 1000)
    );
  });
}

function secondPipeline() {
  return step(async (_, ctx) => {
    return new Promise(resolve =>
      setTimeout(() => {
        resolve('Second pipeline returns this');
      }, 2000)
    );
  });
}

// This resolver returns 'First pipeline returns this'
module.exports = resolver(operations => {
  operations.v1.op1.setup(race(
       firstPipeline(),
       secondPipeline()
  ));
});

Info about Promise.race

Note that promise.race settles on the first promise resolved (be that a value from a resolved promise or an exception from a rejected exception).

Promise.race
Promise.race and async/await examples

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions