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

delay.reject() #7

Closed
sindresorhus opened this issue Dec 24, 2015 · 17 comments
Closed

delay.reject() #7

sindresorhus opened this issue Dec 24, 2015 · 17 comments

Comments

@sindresorhus
Copy link
Owner

Idea from: https://github.com/ariporad/ava/blob/c7b0154f8ae181e892d58ab1d50aee6154a9a743/test/test.js#L7-L12

delay.reject(100, 'message');
delay.reject(100, new Error('message')); // equivalent to the above

If a string, it's converted to an Error, otherwise passed through.

I know the first case is not like Promise.reject(), but is there any good reason not to reject with an error?

@ariporad @jamestalmage Do you think it would be useful to have this here?

@ariporad
Copy link
Contributor

Absolutely. I think this wouldn't be hard to add, and would be rather useful. However, I think that if we were to add this we should also do delay.resolve, and not do the error conversion thing (that makes sense in the context of those AVA tests, but not really in general).

I'm happy to submit a PR if you want.

@sindresorhus
Copy link
Owner Author

However, I think that if we were to add this we should also do delay.resolve

Why? Do you need it for something? I kinda like how I can just write delay(), which is the 95% use-case. .reject() is just an added bonus.

@ariporad
Copy link
Contributor

Completeness. It would be strange to have one and not the other. Plus, it's really trivial to write.

@sindresorhus
Copy link
Owner Author

But now you have two ways of doing the same thing, which I'm not a big fan of. I usually have one default export for the main use-case and then if there's any nice-to-haves, they're put as methods.

Example: https://github.com/sindresorhus/log-update#logupdatetext-

@sindresorhus
Copy link
Owner Author

and not do the error conversion thing (that makes sense in the context of those AVA tests, but not really in general).

Why do think we shouldn't do it? Most will use new Error(), so I thought it would be nice to make the main use-case simpler. And also prevent anyone from shooting themselves in the foot and rejecting a string or something non-error. I think Promise.reject() only supports this for consistency with throw. I don't see why we can't improve the IMHO broken default behavior.

@ariporad
Copy link
Contributor

In regard to the two-ways to do something: Sometimes its worth a little redundancy to avoid confusion down the road.

Imagine that six months ago you wrote the following code:

var delay = require('delay');

// ...

return delay.reject(1000, new Error('foo'));

Without looking at the docs, you'd assume that you can use delay.reject. Now imagine that you're in the same file, and you see this:

// FIXME: every once in a while, this resolves before the promise from #doSomethingElse, and causes problems.
return Promise.reject('foo');

You'd assume that you could just do this:

// FIXME: every once in a while, this resolves before the promise from #doSomethingElse, and causes problems.
return delay.reject(10, 'foo');

@sindresorhus
Copy link
Owner Author

Ok, how about we add it undocumented then?

@jamestalmage
Copy link
Contributor

I am 👍 for the idea. Not sure where i stand on the string to error conversion. Seems a little magic. Also, this is primarily for testing - and if I am accepting user generated promises in my API, correctly handling non-error rejections is a something I want to test.

@ariporad
Copy link
Contributor

Here's three reasons why I wouldn't do that:

  1. Then they'll have to go digging in the source code to figure out why
    their string is being cast to an error.
  2. If it's not documented, it doesn't exist. Semver doesn't apply. Don't
    use it.
  3. No.

@jamestalmage
Copy link
Contributor

👎 for resolve. Keep it simple.

@sindresorhus
Copy link
Owner Author

Ok. No .resolve() (sorry @ariporad, but I'm an extreme minimalist) and no magic string to error conversion. PR welcome :)

@jamestalmage
Copy link
Contributor

What should we do if used like this:

someprise.then(delay.reject(100));

someprise.then(delay.reject(100, new Error()));

@sindresorhus
Copy link
Owner Author

@jamestalmage Same as Promise.reject() I assume? Or do you have a better suggestion?

@jamestalmage
Copy link
Contributor

With delay we just delay and then pass through the result. I think delay.reject(100) with only one arg should delay and reject with whatever it was called with.

You can then do:

somePromise.then(delay(100), delay.reject(100))

To delay resolution and rejection

@ariporad
Copy link
Contributor

Never mind. That makes sense.

@ariporad
Copy link
Contributor

When I'm home in an hour or so I'll submit a PR

@sindresorhus
Copy link
Owner Author

👍 Good idea.

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

No branches or pull requests

3 participants