Skip to content

Instantly share code, notes, and snippets.

@mitchellbusby
Forked from tommarmstrong/Item.js
Last active January 3, 2016 04:21
Show Gist options
  • Save mitchellbusby/41a0663e834a6f4ad682 to your computer and use it in GitHub Desktop.
Save mitchellbusby/41a0663e834a6f4ad682 to your computer and use it in GitHub Desktop.
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;
@mitchellbusby
Copy link
Author

So I'm going to just show you what happens with the propagation of the promise

@mitchellbusby
Copy link
Author

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