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

Broken Promises Flow #19

Open
rzpamidi opened this issue Oct 24, 2017 · 10 comments · May be fixed by #28
Open

Broken Promises Flow #19

rzpamidi opened this issue Oct 24, 2017 · 10 comments · May be fixed by #28
Assignees

Comments

@rzpamidi
Copy link
Contributor

Different behaviour has been noted with the promise returned, when a callback is passed to the api function.

eg.

const instance = new Razorpay({
  key_id: 'rzp_test_2mYdwI91aUdL2u',
  key_secret: 'hd0FgTKnZUUJCGYHTdfQ6Zx2'
});

instance.payments.all({})
  .then((arg) => console.log(arg)) // arg value is printed  
  .catch(arg => console.log(arg))

instance.payments.all({}, function () {
  console.log(arguments); // arguments are printed
}).then((arg) => console.log(arg)) // arg value is undefined 
  .catch(arg => console.log(arg))
@rzpamidi rzpamidi self-assigned this Oct 24, 2017
@selvagsz
Copy link
Contributor

IMO, a promise shouldn't be returned when a callback is passed

@harry1064 harry1064 linked a pull request Dec 28, 2017 that will close this issue
@7ranveer
Copy link

7ranveer commented Feb 10, 2018

The callback function does not return a promise so it will never be resolved in .then() function.
I think below code might work
instance.payments.all({}, function () { console.log(arguments); // arguments are printed return new Promise( (resolve,reject)=>{ if(arguments){ resolve(arguments) }else{ reject("no arguments") } ) }).then((arg) => console.log(arg)) .catch(arg => console.log(arg))

@rzpamidi
Copy link
Contributor Author

rzpamidi commented May 2, 2018

We shall give the same arguments that we passed to callback , to the then callback also, to make this behaviour consistent

@selvagsz
Copy link
Contributor

selvagsz commented May 3, 2018

Before promises, error-first callbacks are the only paradigm to propagate errors thru the async chain. Promises have a declarative approach of error-handling now. It may look weird to resolve a promise with errors as first param

@rzpamidi
Copy link
Contributor Author

rzpamidi commented May 3, 2018

@selvagsz ,

It may look weird to resolve a promise with errors as first param

We will not do that

we support both error-first call back and promises right ? , let the behaviour be consistent that

  1. If you pass a callback , it would get err , data as arguments
  2. If you use Promise Chain , in case of error the promise is rejected else the promise is resolved.

The above points mentioned work well independently.
Let 'em work well together also. Resolving with different arguments when callback is passed is not good.

@selvagsz
Copy link
Contributor

selvagsz commented May 3, 2018

[Question] Why would people do both ? Should we encourage that pattern of using both ?

imo, don't think that's good pattern; callbacks exists for legacy node code. Maybe we can check some other node api wrappers

@rzpamidi
Copy link
Contributor Author

rzpamidi commented May 7, 2018

@selvagsz , node throws an error if we do not provide callback (eg. fs.readFile), which we can not enforce, also all our api's support both, i see no way than supporting both.

We can use the fix i mentioned for backwards compatibility , we can decide later to not return promise when callback is passed, and do Major version bump.

@rzpamidi
Copy link
Contributor Author

rzpamidi commented May 7, 2018

Should we encourage that pattern of using both ?

I don't see any harm , it covers all kinds of users without much overhead , where as the current implementation is inconsistent.

@captn3m0
Copy link
Contributor

@rzpamidi Can we close this?

@rzpamidi
Copy link
Contributor Author

@captn3m0 , need to implement the changes required. will close it after that

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

Successfully merging a pull request may close this issue.

5 participants