Last active
June 25, 2017 12:12
-
-
Save jeffdonthemic/21026a847d92aa810976 to your computer and use it in GitHub Desktop.
Bluebird examplesExamples: http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.htmlProtips: https://gist.github.com/nolanlawson/6ce81186421d2fa109a4Sample: https://github.com/topcoderinc/tco-hackathon/blob/master/routes/index.js
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
var venueCheckins = function(access_token) { | |
return new Promise(function(resolve, reject) { | |
request('https://api.untappd.com/v4/thepub/local?limit=25&lat=61.146283&lng=-149.878679&access_token='+access_token, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var results = JSON.parse(body); | |
var checkins = []; | |
// do magic | |
resolve(checkins); | |
} else { | |
reject(error); | |
} | |
}); | |
}); | |
}; | |
var wishList = function(username, access_token) { | |
return new Promise(function(resolve, reject) { | |
request('https://api.untappd.com/v4/user/wishlist/'+username+'?access_token='+access_token, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var results = JSON.parse(body); | |
var wishList = []; | |
// do magic | |
resolve(wishList); | |
} else { | |
reject(error); | |
} | |
}); | |
}); | |
}; | |
// venueCheckins(req.cookies.access_token).then(function(checkins) { | |
// //console.log(checkins); | |
// console.log('done with checkins'); | |
// }); | |
// | |
// wishList(req.params.username, req.cookies.access_token).then(function(beers) { | |
// //console.log(checkins); | |
// console.log(beers); | |
// }); | |
Promise.join( | |
venueCheckins(req.cookies.access_token), | |
wishList(req.params.username, req.cookies.access_token) | |
).then(function(data) { | |
console.log(data[0]); | |
console.log(data[1]); | |
}).catch(function(e) { | |
console.log(e); | |
}); | |
clover.items() | |
.then(function(results) { | |
console.log(results); | |
req.flash('info', 'Successful sync of item data with Clover'); | |
}).catch(function(e) { | |
req.flash('error', 'Could not get all items from Clover. See console.'); | |
}).finally(function(){ | |
res.render('sync/index'); | |
}); | |
// ======================= | |
Event.findByIdAsync(req.params.eventId).then(function(event) { | |
var team = _.find(event.teams, function (t) { | |
return t.id === req.params.teamId; | |
}); | |
console.log("found team!! " + team.name); | |
return team; | |
}).then(function(team) { | |
console.log(team); | |
Submission.find({ 'team': req.params.teamId }) | |
.select('repoUrl video') | |
.exec(function(err, submission) { | |
console.log(submission) | |
}) | |
}).catch(function(e) { | |
console.log('======= ERROR'); | |
console.log(e); | |
}) | |
// =================== | |
var eventAndTeam = Promise.join( | |
Event.findByIdAsync(req.params.eventId).then(function(event) { | |
var team = _.find(event.teams, function (t) { | |
return t.id === req.params.teamId; | |
}); | |
return team; | |
}), | |
Submission.find({ 'team': req.params.teamId }) | |
.select('repoUrl video') | |
.exec(), | |
function (event, team) { | |
var r = []; | |
r.push(event); | |
r.push(team); | |
return r; | |
} | |
); | |
eventAndTeam.then(function(results) { | |
console.log('========== all'); | |
console.log(results) | |
}); | |
====================== | |
new Promise(function (resolve, reject) { | |
fs.readFile('myfile.txt', function (err, file) { | |
if (err) { | |
return reject(err); | |
} | |
resolve(file); | |
}); | |
}).then(/* ... */) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment