Last active
May 2, 2017 02:12
-
-
Save kishanio/81b5adee4f1dd998b5eb9458487caadb to your computer and use it in GitHub Desktop.
Parse : Chaining Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parse : Chaining Promises in Parallel | |
// File : utils/api.js | |
var Api = {}; | |
Api.Count = function() { | |
var promises = []; | |
var shelf = new Parse.Query(Shelf); | |
var note = new Parse.Query(Note); | |
var book = new Parse.Query(Book); | |
promises.push(shelf.count()); | |
promises.push(book.count()); | |
promises.push(note.count()); | |
return Parse.Promise.when(promises); | |
} | |
export default Api; | |
// Usage | |
import Api from 'utils/Api'; | |
Api.Count().then(function(counts){ | |
this.setState({ | |
counts: counts | |
}); | |
}.bind(this)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parse : Chaining Promises in Series | |
// File : utils/api.js | |
var Api = {}; | |
Api.NewNote = function(note,file) { | |
var file = new Parse.File(file.name, file); | |
return file.save().then(function(file) { | |
var promise = Parse.Promise.as(); | |
promise = promise.then( function() { | |
var NewNote = new Note(); | |
if ( file.name ) { | |
NewNote.set('file', file); | |
} | |
return NewNote.save(note); | |
}); | |
return promise; | |
}); | |
} | |
export default Api; | |
// Usage | |
import Api from 'utils/Api'; | |
Api.NewNote(newNote, this.state.file).then(function(note){ | |
this.resetState(); | |
this.props.onNewNote(note); | |
}.bind(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment