justsml / escape-from-callback-mountain Goto Github PK
View Code? Open in Web Editor NEWExample Project & Guide for mastering Promises in Node/JavaScript. Feat. proposed 'Functional River' pattern
License: MIT License
Example Project & Guide for mastering Promises in Node/JavaScript. Feat. proposed 'Functional River' pattern
License: MIT License
Demonstrate a few ways to use closures for sharing variables over boundaries.
Don't write code like this - ok if you cant re-order function calls, or a use a context-free wrapper function. I sometimes fallback to this technique if I have a legacy API, or other design challenge (e.g. EventEmitter with several paths for success & errors).
To prevent prematurely creating Promises and exhausting resurces, we'll start our Promise chain by simply passing our parent function's params (ids
array) into Bluebird .map
handler calling our api method - now with sane limits:
// In order to avoid overwhelming `api.getById` - for example lets assume we are limited to 4 concurrent connections we can easily add or remove this kind of real-world requirement
const getUsersById = (ids = []) => Promise.resolve(ids)
.map(api.getById, {concurrency: 4})
// Call getUsers, but attach logic to extract names out
const userNames = getUsersById([1, 2, 3])
.all().spread((users) => users.map(u => u.name))
userNames.then(console.log)) // > ['Emily', 'Mel', 'Paris']
// Or reuse Promise w/ built-in memoization.
// +1 for caching lookups + disposing of objects in RAM using JS scope & garbage collection.
const getUserNamesAndActivity = (userIds) => {
const users = getUsersById(userIds)
const userActivity = users.map(({username}) => api.getTopActivity({username}), {concurrency: 4}) // throttle net-bound API calls
const userNames = users.map(({name}) => name) // no I/O or net-bound call: don't throttle
return Promise.props({userNames, userActivity});
}
This "requirement" has a funny way of coming up really late in the game, maybe even in production or perhaps after a massive spike in traffic.
Avoid issues caused by "launching" too many Promises at once: simply 'start' with an array of primitive values or with something like: Promise.resolve(Array.from({length: 5000}))
Then you just need to call .map
and specify the concurrency
limit, like so:
Promise.resolve(Array.from({length: 5000}))
.map((x, idx) => api.activityByIndex(idx))
Add rich media demos!
Fix different annotations & background color on README's screenshots.
If you have any feedback about how native promises can give users a better debugging experience please let us know :) We're gathering use cases for Node.js to see what we can do better.
Using filtering with Bluebird's .catch w/ 2 params like:
Promise.resolve()
.catch(NetworkError, _fatalError)
.catch(NetworkError, _fatalError)
...
Using methods from http://reactivex.io/documentation/operators/flatmap.html and
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.