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
// with the Bluebird library | |
import {props, all, coroutine} from 'bluebird'; | |
// with objects | |
coroutine(function*() { | |
const {user1, user2, user3} = yield props({ | |
user1: user.findOneAsync({name: "Will"}), | |
user2: user.findOneAsync({name: "Adam"}), | |
user3: user.findOneAsync({name: "Ben"}) | |
}); |
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 co from 'co'; | |
// with objects | |
co(function*() { | |
const {user1, user2, user3} = yield { | |
user1: user.findOneAsync({name: "Will"}), | |
user2: user.findOneAsync({name: "Adam"}), | |
user3: user.findOneAsync({name: "Ben"}) | |
}; | |
).catch(err => handleError(err)) |
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 co from 'co'; | |
app.post("/purchase", (req, res) => { | |
co(function* () { | |
const person = yield user.findOneAsync(req.body); | |
const permissions = yield permissions.findAllAsync(person); | |
if (isAllowed(permissions)) { | |
const confirmNum = yield transaction.processAsync(user); | |
res.send("Your transaction was successful!") | |
} | |
}).catch(err => handleError(err)) |
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
app.post("/purchase", async function (req, res) { | |
const userData = await user.findOneAsync(req.body); | |
const permissions = await permissions.findAllAsync(userData); | |
if (isAllowed(permissions)) { | |
const confirmNum = await transaction.processAsync(userData); | |
res.send("Your purchase was successful!") | |
} | |
}); |
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
app.post("/purchase", (req, res) => { | |
user.findOneAsync(req.body) | |
.then( userData => { | |
return permissions | |
.findAllAsync(userData) | |
.then( permissions => { | |
if (isAllowed(permissions)) { | |
return transaction.processAsync(userData); | |
} | |
}); |
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
// all asynchronous methods have been promisified | |
app.post("/purchase", (req, res) => { | |
user.findOneAsync(req.body) | |
.then( userData => permissions.findAllAsync(userData) ) | |
.then( permissions => { | |
if (isAllowed(permissions)) { | |
return transaction.processAsync(userData); | |
// userData is not defined! It's not in the proper scope! | |
} | |
}) |
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
app.post("/purchase", (req, res) => { | |
user.findOne(req.body, (err, userData) => { | |
if (err) return handleError(err); | |
permissions.findAll(userData, (err2, permissions) => { | |
if (err2) return handleError(err2); | |
if (isAllowed(permissions)) { | |
transaction.process(userData, (err3, confirmNum) => { | |
if (err3) return handleError(err3); | |
res.send("Your purchase was successful!"); | |
}); |
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
user.findOne({name: "Will"}, (err, user) => { | |
if (err) return handleError(err); | |
permissions.findAll({id: user.id}, (err2, permissions) => { | |
if (err2) return handleError(err2); | |
if (isAllowed(permissions)) { | |
transaction.process(user, (err3, confirmNum) => { | |
if (err3) handleError(err3); | |
res.send("Your transaction was successful!) | |
}) | |
}) |
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
// medium_example_app/controllers/index_controller.js | |
// import your helpers/middleware/models here | |
export default function (router) { | |
router.use(/* controller specific middleware goes here */); | |
router.get('/', (req, res) => { | |
// call model methods to get JSON from db | |
let data = {data: 'hello world'}; | |
res.json(data); |
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
{ | |
"name": "medium_example_app", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", |