Skip to content

Latest commit

 

History

History
105 lines (75 loc) · 2.19 KB

2.0.0.md

File metadata and controls

105 lines (75 loc) · 2.19 KB

Upgrading Parse SDK to version 2.0.0

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:

.done() and .fail() continuations

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) => {

});

.always() is replaced by .finally()

// before
const query = new Parse.Query();
query.find()
.always((result) => {

});

// after
query.find()
.finally((result) => {

});

new Parse.Promise()

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); }
  });
});

Static methods

  • Parse.Promise.as is replaced by Promise.resolve
  • Parse.Promise.error is replaced by Promise.reject
  • Parse.Promise.when is replaced by Promise.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]) => {

});

⚠️ Whereas 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