Skip to content

Commit 6fbc075

Browse files
DOCSP-27527 Remove callbacks (#493)
1 parent 5237fd4 commit 6fbc075

File tree

4 files changed

+19
-294
lines changed

4 files changed

+19
-294
lines changed

source/code-snippets/transactions/txn-callback.js

Lines changed: 0 additions & 147 deletions
This file was deleted.

source/fundamentals/promises.txt

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
.. _node-promises-and-callbacks:
1+
.. _node-promises:
22

3-
======================
4-
Promises and Callbacks
5-
======================
3+
========
4+
Promises
5+
========
66

77
.. default-domain:: mongodb
88

@@ -25,8 +25,7 @@ executing long-running operations. For more information about asynchronous
2525
Javascript, see the MDN web documentation on
2626
`Asynchronous Javascript <https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous>`_.
2727

28-
This section describes two features of asynchronous Javascript --
29-
``Promises`` and ``Callbacks`` -- that you can use with the Node.js driver to
28+
This section describes ``Promises`` that you can use with the Node.js driver to
3029
access the results of your method calls to your MongoDB cluster.
3130

3231
Promises
@@ -125,52 +124,6 @@ chaining example.
125124
For additional information, see the MDN documentation on
126125
:mdn:`await <Web/JavaScript/Reference/Operators/await>`.
127126

128-
129-
Callbacks (Deprecated)
130-
----------------------
131-
132-
.. important::
133-
134-
Callbacks are deprecated in the {+driver-short+} version 4.10 and will
135-
be removed in a future release. We recommend that you use Promises
136-
or Async/Await instead.
137-
138-
If your application requires the use of callbacks, you can use the
139-
{+node-legacy+}, which wraps the {+driver-short+} and provides
140-
optional callback support.
141-
142-
A callback is a method that gets called after another method has
143-
finished executing. This allows the enclosing method to continue to execute
144-
other commands until the original operation completes. Callbacks are often
145-
used to enforce the order of processing commands.
146-
147-
In the MongoDB Node.js driver, you can optionally declare a callback method to
148-
async operations that normally return Promises. Once the operation completes
149-
execution, the callback method executes as shown in the following code
150-
snippet:
151-
152-
.. code-block:: js
153-
154-
collection.findOneAndUpdate(
155-
{ name: "Barronette Peak" },
156-
{ $set: { name: "Baronette Peak" } },
157-
{},
158-
function(error, result) {
159-
if (!error) {
160-
console.log(`Operation completed successfully: ${result.ok}`);
161-
} else {
162-
console.log(`An error occurred: ${error}`);
163-
}
164-
},
165-
);
166-
167-
For more information on the callback method signature for the specific
168-
driver method, see the `API documentation <{+api+}>`__.
169-
170-
.. note::
171-
172-
If you specify a callback, the method *does not* return a Promise.
173-
174127
Operational Considerations
175128
--------------------------
176129

@@ -248,9 +201,3 @@ in the following code:
248201
console.log(await cursor.next());
249202
}
250203
}
251-
252-
.. note::
253-
254-
For additional information on using Promises and Callbacks with the MongoDB
255-
Node.js driver, see this MongoDB University course video on `asynchronous
256-
Javascript programming <https://youtu.be/R4AEyKehpss>`_.

source/fundamentals/transactions.txt

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ MongoDB in the following sections of this guide:
6464
Transaction APIs
6565
----------------
6666

67-
To implement a transaction with the driver, you can use either the
68-
**Core API** or the **Callback API**. To use the Core API, you declare the
69-
start and commit points of the transaction. To use the Callback API, you pass
70-
a callback function that contains the logic you want to run as a transaction.
67+
Use the **Core API** to implement a transaction with the driver. To use the Core
68+
API, you declare the start and commit points of the transaction.
7169

7270
Core API
7371
~~~~~~~~
@@ -93,34 +91,6 @@ method on the ``Session`` object:
9391
See the :ref:`Core API example <nodejs-transaction-core-api-example>` for
9492
a sample transaction implementation.
9593

96-
Callback API
97-
~~~~~~~~~~~~
98-
99-
.. important::
100-
101-
The Callback API is deprecated in {+driver-short+} version 4.10 and will
102-
be removed in a future release. We recommend that you use the
103-
:ref:`Core API <nodejs-transaction-core-api-example>`.
104-
105-
The Callback API enables you to run a transaction by passing a callback
106-
function that encapsulates the series of operations that make up the
107-
transaction. This API automatically handles certain transaction errors
108-
returned by the server.
109-
110-
See
111-
:manual:`TransientTransactionError </core/transactions-in-applications/#std-label-transient-transaction-error>`
112-
and
113-
:manual:`UnknownTransactionCommitResult </core/transactions-in-applications/#-unknowntransactioncommitresult->`
114-
for more information on these errors.
115-
116-
To create a transaction, pass a callback function that encapsulates your
117-
transaction logic to the ``withTransaction()`` method on a ``Session``
118-
object. The driver automatically retries the transaction when it encounters
119-
certain errors reported by the server.
120-
121-
See the :ref:`Callback API example <nodejs-transaction-callback-api-example>`
122-
for a sample transaction implementation.
123-
12494
.. _nodejs-transaction-settings:
12595

12696
Transaction Settings
@@ -171,21 +141,6 @@ resembles the following:
171141
};
172142
session.startTransaction(transactionOptions);
173143

174-
You can specify the transaction options in the Callback API using code that
175-
resembles the following:
176-
177-
.. code-block:: javascript
178-
179-
const transactionOptions = {
180-
readPreference: 'primary',
181-
readConcern: { level: 'local' },
182-
writeConcern: { w: 'majority' },
183-
maxCommitTimeMS: 1000
184-
};
185-
await session.withTransaction(
186-
async (session) => { /* your transaction here */ },
187-
transactionOptions);
188-
189144
.. _nodejs-transaction-examples:
190145

191146
Examples
@@ -313,48 +268,6 @@ See the :ref:`Payment Transaction Result <nodejs-transaction-example-payment-res
313268
section to see what your collections should contain after you run the
314269
transaction.
315270

316-
.. _nodejs-transaction-callback-api-example:
317-
318-
Callback API Implementation
319-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
320-
321-
.. important::
322-
323-
The Callback API is deprecated in {+driver-short+} version 4.10 and will
324-
be removed in a future release. We recommend that you use the
325-
:ref:`Core API <nodejs-transaction-core-api-example>`.
326-
327-
The code examples in this section show how you can use the Callback API to
328-
run the multi-document payment transaction in a session.
329-
330-
The following code example shows how you can start the session and pass an
331-
anonymous callback function that contains the operations you want to run
332-
as a transaction.
333-
334-
.. literalinclude:: /code-snippets/transactions/txn-callback.js
335-
:language: javascript
336-
:linenos:
337-
:dedent:
338-
:emphasize-lines: 1-6,8,10-12,16
339-
:start-after: start session
340-
:end-before: end session
341-
342-
Rather than pass an anonymous callback function, you can pass a named
343-
function that returns a Promise. See the following ``placeOrder()``
344-
example callback function that you can pass to ``withTransaction()`` to run
345-
as a transaction:
346-
347-
.. literalinclude:: /code-snippets/transactions/txn-callback.js
348-
:language: javascript
349-
:linenos:
350-
:emphasize-lines: 9,22,33,41
351-
:start-after: start callback
352-
:end-before: end callback
353-
354-
See the :ref:`Payment Transaction Result <nodejs-transaction-example-payment-result>`
355-
section to see what your collections should contain after you run the
356-
transaction.
357-
358271
.. _nodejs-transaction-example-payment-result:
359272

360273
Payment Transaction Result

source/whats-new.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ What's New in 5.0
4141

4242
New features of the 5.0 {+driver-short+} release include:
4343

44+
.. important:: Breaking Change: Removal of Callback Support
45+
46+
This release removes support for callbacks in favor of a promise-based API.
47+
The following list provides some strategies for callback users to adopt this
48+
version:
49+
50+
- Migrate to the promise-based API (recommended)
51+
- Use the promise-based API and ``util.callbackify``
52+
- Add ``mongodb-legacy`` to continue using callbacks
53+
54+
For more information about these strategies, see the `v5 Change Notes <https://github.com/mongodb/node-mongodb-native/blob/main/etc/notes/CHANGES_5.0.0.md>`__.
55+
4456
- By default, the driver no longer checks types referenced in dot notation
4557
unless the ``StrictFilter`` type annotation is explicitly
4658
used. To learn more about this change, see the :ref:`Typescript fundamentals

0 commit comments

Comments
 (0)