question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transaction API misleading

See original GitHub issue

First off thanks for Knex! 😃

The transaction API has a promise-like API (then(), catch()) that suggests it’s exposing a promise for the eventual committal or rollback of the transaction. Instead, however, it’s a stateful API with nothing to do with promises - every time you call it something happens and a different promise is returned!

If you consider the root idea of promises it is that they represent a future value, or a failure to provide that value. Calls to then() are entirely divorced from time - e.g you should be able to call then() any number of times, before, after, and during the asynchronous operation it represents, and get the same result every time.

I’d be happy to contribute to fix up this aspect of the API. I just wasted a heap of time because something that looked like a promise wasn’t one.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:18 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
bruuncommented, Jul 6, 2015

I just wasted a heap of time because something that looked like a promise wasn’t one.

You are not alone. A .then in promise-land shouldn’t have side-effects, but the knex API does.

1reaction
timrufflescommented, Sep 9, 2014

A transaction is either committed or rejected, so seems like good mapping to promise semantics. Each transaction is triggered via commit() at some future point, so it could create a deferred inside its constructor and expose its promise via then().

On the query front, it’s clearly trickier as you want to be able to reuse the same builder. A solution is to put the execution behaviour behind something like an run() method: no more verbose and makes the builder/query separation clear:

// a builder
var userSelect = knex.select('name')
  .from('users');

// promises for the result of a query
var oldUsers = userSelect
  .where('age > ?', 90)
  .run();

var youngUsers = userSelect
  .where('age < ?', 25)
  .run();

var oldAndYoungUsers = Promise.all([oldUsers, youngUsers])
  .then(_.flatten);

Having things exposing a then() without promise semantics will just get more confusing as time goes on and promises are more widely used/understood (i.e ES6 adoption).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Transaction API misleading · Issue #470 · knex/knex - GitHub
The transaction API has a promise-like API ( then() , catch() ) that suggests it's exposing a promise for the eventual committal or...
Read more >
Millions of Venmo transactions scraped in warning over ...
A computer science student has scraped seven million Venmo transactions to prove that users' public activity can still be easily obtained, ...
Read more >
Shopify Payments Duplicate API / Wrong CCV Transaction - Shopify ...
We are integrating Shopify Payments with ShipEdge and receiving duplicate entries in the gateway field when a credit card is rejected and then...
Read more >
DEPRECATED - Transactions API: How It Works
If delay_capture is false, the payment is fully processed and credited to the associated Square account. Charge returns a transaction object with a...
Read more >
Shopify Transactions API - wrong gateway name displaying ...
I am sending a POST to capture multiple payment gateways: A Gift Card(Custom Payment Gateway) and a Credit Card (Stripe). My Stripe capture ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found