Skip to content

Instantly share code, notes, and snippets.

@ghmeier ghmeier/dist-index.js
Last active Nov 4, 2017

Embed
What would you like to do?
Promise Patterns
// This file is generated by babel-plugin-transform-async-to-generator
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function foo() {
var _this = this;
return _asyncToGenerator(function* () {
// Uses yield instead of await.
return true;
});
}
{
// Entrypoint should be your shim file.
"main": "index.js",
// Include files that should be published.
"files": [
"index.js",
"dist",
"src"
],
"dependencies": {
"semver": "^5.4.1"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-plugin-transform-async-to-generator": "^6.24.1"
},
// Now you can support any node version > 6.
"engines": {
"node": ">= 6.9.1"
},
// Add the build script and prepublish script
"scripts": {
"build": "babel src/ -d dist/",
"prepublish": "yarn run build"
}
}
const semver = require('semver');
let main;
if (semver.lt(process.version, '7.6.0')) {
// Use built entrypoint for any version below 7.6.0 since
// all other versions don't natively support async functions.
// http://node.green/#ES2017-features-async-functions
main = require('dist-index.js');
} else {
main = require('src-index.js');
}
module.exports = main;
// Automatically set up your project to be transpiled using:
// node migrate-module.js <src-dir>
const fs = require('fs');
const srcFolder = process.argv[2];
if (!srcFolder) {
console.log('Please provide the name of your src folder.');
process.exit(1);
}
const raw = fs.readFileSync('./package.json');
const pkg = JSON.parse(raw);
pkg.main = 'index.js';
if (!pkg.engines) pkg.engines = {};
pkg.engines.node = '>= 6.9.1';
if (!pkg.scripts) pkg.scripts = {};
pkg.scripts.babelBuild = `babel ${srcFolder} -d dist`;
const buildCmd = 'yarn run babelBuild';
if (pkg.scripts.prepublish) {
pkg.scripts.prepublish = buildCmd + ' && ' + pkg.scripts.prepublish;
} else {
pkg.scripts.prepublish = buildCmd;
}
// Explicitly add build files
if (!pkg.files) pkg.files = [];
pkg.files.push('index.js', 'src', 'dist');
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));
process.exit(0);
// Handle lists with Promise.all and Array.map
async function addToList(emails) {
await Promise.all(emails.map(async (email) => {
const valid = await validate(email);
if (!valid) return;
await db.list.insert({ email });
return email;
}));
}
// Promisify just one call.
const { deferred } = require('promise-callbacks');
function bar() {
const promise = deferred();
doAsync(promise.defer());
return promise;
}
// Promisify whole apis.
const { promisify } = require('promise-callbacks');
const api = {
doAsync: (cb) => {},
doMoreAsync: (cb) => {}
};
const doPromisified = promisify.all(api);
doPromisified.doAsync.then(() => {
// Celebrate
});
// Any iterator with a next method will do.
class Iterator {
next() {
if (!this.results) this.results = this.get();
return results.pop();
}
}
// Use promise iterate to defer async calls to the iterator without
// loading all results at once.
const { promiseIterate } = require('promise-iterate');
async function sendToEveryone(names) {
const cursor = new Iterator();
await promiseIterate(cursor, async (contact) => {
await sendEmail(contact.firstName, 'Hello!');
});
return true;
}
// Use promise pool for arbitrary length data to limit memory usage
// and active requests.
const PromisePool = require('@mixmaxhq/promise-pool');
async function bulkSend(names) {
const pool = new PromisePool(10);
for (let i = 0; i < names.length; i++) {
const name = names[i];
await pool.start(async () => {
send(name, 'Hello!');
});
}
await pool.flush();
return { message: 'success' };
}
async function foo() {
// Some async action.
return true;
}
module.exports = {
foo
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.