gulpjs / async-done

Allows libraries to handle various caller provided asynchronous functions uniformly. Maps promises, observables, child processes and streams, and callbacks to callback style.
MIT License
69 stars 21 forks source link

Added support for returning the last state of an observable. #28

Closed shannonmoeller closed 8 years ago

shannonmoeller commented 8 years ago

Tried to keep it minimal and framework agnostic. Tests passed without change (only dropped the .skip).

phated commented 8 years ago

That's a neat trick for storing the next value. Do you have a use case for this functionality?

shannonmoeller commented 8 years ago

I've written a map-stream implementation where async-done is used to resolve the mapping function. This allows transform stream authors to choose whatever async pattern suits their fancy:

https://github.com/shannonmoeller/async-map-stream/blob/4f81354c2128471375078125ce630d2da5d2e389/test/asyncMapStream.spec.js#L29-L54

phated commented 8 years ago

That's awesome. Merging :100:

shannonmoeller commented 8 years ago

Thank you, sir!

phated commented 8 years ago

Published as 1.2.0 - Thanks for the awesome implementation

tunnckoCore commented 8 years ago

Cool. What I did was detecting ret.value instead of .state from onNext. Is it better or? Thoughts?

for example: few lines from letta-value and tests are currently in always-done (always-done pass 100% of async-done tests)

Thanks.

shannonmoeller commented 8 years ago

@tunnckoCore I'm not sure what you mean. I only used onNext.state as a convenient place to store a value that is specific to the observable use case. Could have stored it in a scope variable, but I liked how this kept things tidy.

tunnckoCore commented 8 years ago

@shannonmoeller nevermind. I'm just saying that i think this thing can be done like that

   if(result && typeof result.subscribe === 'function'){
      if (result.value) return done(null, result.value)
      // assume RxJS observable
      result.subscribe(noop, onError, onSuccess);
      return;
    }

Nevermind :) Both ways seems to work.

shannonmoeller commented 8 years ago

@tunnckoCore Using result.value in that way would only work with truthy values, but storing the latest value received by the onNext handler is guaranteed to be the right thing.

Also, is .value a standard observable thing? I can't find any documentation on it.

tunnckoCore commented 8 years ago

in that way would only work with truthy values

haha, good catch, i should fix it.

I can't find any documentation on it.

Same here. I just noticed that it is the thing that is needed.

Also, is .value a standard observable thing?

I'm not sure, I'm not familiar with RX.