With Parse SDK 2.0.0, gone are the backbone style callbacks and Parse.Promises.
Instead, the Parse SDK 2.0.0 uses native promises in the browser, node and react native that allows for a more standard API.
Migrating to native Promises should be straightforward, we'll recap the different changes:
With native promises, .done
and .fail
don't exist and are replaces by .then
and .catch
// before
const query = new Parse.Query();
query.find()
.done((results) => {
})
.fail((error) => {
});
// after
query.find()
.then((results) => {
})
.catch((error) => {
});
// before
const query = new Parse.Query();
query.find()
.always((result) => {
});
// after
query.find()
.finally((result) => {
});
With native promises, the constructor is different, you will need to update to the native constructor which requires moving the code around.
// before
const promise = new Parse.Promise();
doSomethingAsync((error, success) => {
if (error) { promise.reject(error); }
else { promise.resolve(success); }
});
return promise;
// after
return new Promise((resolve, reject) => {
doSomethingAsync((error, success) => {
if (error) { reject(error); }
else { resolve(success); }
});
});
Parse.Promise.as
is replaced byPromise.resolve
Parse.Promise.error
is replaced byPromise.reject
Parse.Promise.when
is replaced byPromise.all
Promise.all
only takes an array or an iterable promises.
// before
Parse.Promise.when(promise1, promise2, promise3)
.then((result1, result2, result3) => {
});
// after
Promise.all([promise1, promise2, promise3])
.then(([result1, result2, result3]) => {
});
Parse.Promise.always
, Promise.finally
callback don't receive any arguments, and don't resolve with a new argument to pass to the next promise in chain.
// before
Parse.Promise.as(1).always((val) => val + 1).then((result) => console.log(result))
// will print 2
// after
Promise.resolve(1).finally(() => 2).then((result) => console.log(result))
// will print 1