Before upgrading to v2, if you are using the callbacks variant of v1, you must switch to the promise-based variant.
1. Change the import/instantiation:
require('ably/callbacks') $\rightarrow$ require('ably/promises')new Ably.Realtime.Callbacks(...) $\rightarrow$ new Ably.Realtime.Promise(...)
2. Update method calls:
Methods that previously took a callback (err, result) as the last argument should now be called using await or .then()/.catch().
// v1 Callbacks style
channel.history({ direction: 'forwards' }, (err, paginatedResult) => {
if (err) return;
// use paginatedResult
});
// v1 Promises style (Required before v2 upgrade)
// Option A: async/await
try {
const paginatedResult = await channel.history({ direction: 'forwards' });
} catch (err) {
// handle error
}
// Option B: .then()
channel.history({ direction: 'forwards' })
.then((paginatedResult) => { /* use result */ })
.catch((err) => { /* handle error */ });
Note: In v1, Crypto.generateRandomKey() is an exception; it remains callback-based even in the v1 promise variant. In v2, it becomes promise-based.
// v1 Callbacks style
channel.history({ direction: 'forwards' }, (err, paginatedResult) => {
if (err) {
// Perform some sort of error handling
return;
}
// Make use of paginatedResult
});
// v1 Promises style (Required before v2 upgrade)
// Option A: async/await
try {
const paginatedResult = await channel.history({ direction: 'forwards' });
// Make use of paginatedResult
} catch (err) {
// Perform some sort of error handling
}
// Option B: .then()
channel
.history({ direction: 'forwards' })
.then((paginatedResult) => {
// Make use of paginatedResult
})
.catch((err) => {
// Perform some sort of error handling
});