-
-
Save mitchellbusby/41a0663e834a6f4ad682 to your computer and use it in GitHub Desktop.
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
import db from '../database'; | |
class Item { | |
static fetch(user_id) { | |
return // make sure you return the promise so it can be seen by the outer method! | |
db.query("select * from items where user_id = $1", user_id) | |
.then(function (result) { | |
// This result object obvs comes from the $promise.resolve(result); that would happen within query. | |
// Lets pretend result is the string "Hello world" | |
console.log(result); | |
// prints "Hello world" | |
// You can then propagate that result, or shift it or do whatever you want, or you could return it | |
// unaltered. in this case we'll return uppercased :P | |
return result.toUpperCase(); | |
}) | |
.catch(function (error) { | |
console.log(error) | |
// Do a similar thing with the error, you can also do other things like resolve it here probs | |
}); | |
} | |
} | |
// Now lets see what happens when we do this Item.fetch, I ceebs instantiating though but pretend I did | |
Item.fetch(5) | |
.then(function(result) { | |
console.log(result); | |
// Outputs "HELLO WORLD" | |
}); | |
export default Item; |
Also a fantastic tutorial on the ins and outs of promise, I highly recommend it alongside this code fragment http://www.html5rocks.com/en/tutorials/es6/promises/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So I'm going to just show you what happens with the propagation of the promise