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

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

Open
Xavier-Redondo opened this issue Jan 31, 2023 · 0 comments
Labels

Comments

@Xavier-Redondo
Copy link
Contributor

Xavier-Redondo commented Jan 31, 2023

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

@Xavier-Redondo Xavier-Redondo added bautajs-core 3.x Target release 3.x good first issue Good for newcomers labels Jan 31, 2023
@franher franher removed the 3.x Target release 3.x label Oct 28, 2023
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

2 participants